Skip to contentSkip to conversation

Avoid cpu_physical_memory_read() in generic code

PATCH Superseded Peter Maydell 3 participants 8 messages Nov 22, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Avoid cpu_physical_memory_read() in generic code

This patchset takes three places in generic code which use cpu_physical_memory_read(), and changes them to use address_space_read() instead.

cpu_physical_memory_{read,rw,write} all implicitly assume that there is exactly one view of physical memory. This is sort-of true today, but we'd like to be able to move to having heterogenous systems where not all CPUs share the same view of physical memory.

In disas.c we are disassembling for a particular CPU, so use that CPU's primary address space (cs->as).

In monitor.c we are reading physical memory for a particular CPU, so again use that CPU's primary address space; we fall back to address_space_memory for the case where there are no CPUs in the system (-machine none).

In elf_ops.h the function was passed an address space to use, so just use it.

Other places in generic code that use these functions are:
* dump.c -- the whole UI here seems to assume that there is only one view of memory and that is what is being dumped
* cpu.c:qmp_pmemsave() -- again, the UI assumption is that there's only one view of memory
So I've left those alone.

NB: git grep command line for finding callsites:
git grep '\<cpu_physical_memory_\(read\|write\|rw\)\>'

thanks
-- PMM

Peter Maydell (3):
disas.c: Use address_space_read() to read memory
monitor: Use address_space_read() to read memory elf_ops.h: Use address_space_write() to write memory

 include/hw/elf_ops.h | 3 ++-
 disas.c              | 5 ++++-
 monitor.c            | 8 +++++++-
 3 files changed, 13 insertions(+), 3 deletions(-)
Signature · 1 line
2.19.1
#1 lore raw

PATCH 1/3 disas.c: Use address_space_read() to read memory

Currently disas.c reads physical memory using cpu_physical_memory_read(). This effectively hard-codes assuming that all CPUs have the same view of physical memory. Switch to address_space_read() instead, which lets us use the AddressSpace for the CPU we're disassembling for.

---
 disas.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
disas.c +4 -1
index 5325b7e6be6..f9c517b3588 100644
@@ -588,7 +588,10 @@ static int
 physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
                      struct disassemble_info *info)
 {
-    cpu_physical_memory_read(memaddr, myaddr, length);
+    CPUDebug *s = container_of(info, CPUDebug, info);
+
+    address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED,
+                       myaddr, length);
     return 0;
 }
 
Signature · 1 line
2.19.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH for-4.0 1/3] disas.c: Use address_space_read() to read memory

Quoted 8 lines · Peter Maydell

Currently disas.c reads physical memory using cpu_physical_memory_read(). This effectively hard-codes assuming that all CPUs have the same view of physical memory. Switch to address_space_read() instead, which lets us use the AddressSpace for the CPU we're disassembling for.

Quoted 21 lines
---
 disas.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
disas.c +4 -1
index 5325b7e6be6..f9c517b3588 100644
@@ -588,7 +588,10 @@ static int
 physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
                      struct disassemble_info *info)
 {
-    cpu_physical_memory_read(memaddr, myaddr, length);
+    CPUDebug *s = container_of(info, CPUDebug, info);
+
+    address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED,
+                       myaddr, length);
     return 0;
 }
 
#3 lore raw

PATCH 2/3 monitor: Use address_space_read() to read memory

In reply to Peter Maydell

Currently monitor.c reads physical memory using cpu_physical_memory_read(). This effectively hard-codes assuming that all CPUs have the same view of physical memory. Switch to address_space_read() instead, which lets us use the AddressSpace for the CPU we're reading memory for (falling back to address_space_memory if there is no CPU, as happens with the "none" board). As a bonus, this allows us to detect failures to read memory.

---
 monitor.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
monitor.c +7 -1
index d39390c2f2f..b0e8f2c490a 100644
@@ -1604,7 +1604,13 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
         if (l > line_size)
             l = line_size;
         if (is_physical) {
-            cpu_physical_memory_read(addr, buf, l);
+            AddressSpace *as = cs ? cs->as : &address_space_memory;
+            MemTxResult r = address_space_read(as, addr,
+                                               MEMTXATTRS_UNSPECIFIED, buf, l);
+            if (r != MEMTX_OK) {
+                monitor_printf(mon, " Cannot access memory\n");
+                break;
+            }
         } else {
             if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
                 monitor_printf(mon, " Cannot access memory\n");
Signature · 1 line
2.19.1
Reviewed-by on v1 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH for-4.0 2/3] monitor: Use address_space_read() to read memory

Quoted 10 lines · * Peter Maydell ([email protected]) wrote:

Currently monitor.c reads physical memory using cpu_physical_memory_read(). This effectively hard-codes assuming that all CPUs have the same view of physical memory. Switch to address_space_read() instead, which lets us use the AddressSpace for the CPU we're reading memory for (falling back to address_space_memory if there is no CPU, as happens with the "none" board). As a bonus, this allows us to detect failures to read memory.

Yes that's nice; there's probably already a couple of CPUs that show differences.

Quoted 26 lines
---
 monitor.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
monitor.c +7 -1
index d39390c2f2f..b0e8f2c490a 100644
@@ -1604,7 +1604,13 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
         if (l > line_size)
             l = line_size;
         if (is_physical) {
-            cpu_physical_memory_read(addr, buf, l);
+            AddressSpace *as = cs ? cs->as : &address_space_memory;
+            MemTxResult r = address_space_read(as, addr,
+                                               MEMTXATTRS_UNSPECIFIED, buf, l);
+            if (r != MEMTX_OK) {
+                monitor_printf(mon, " Cannot access memory\n");
+                break;
+            }
         } else {
             if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
                 monitor_printf(mon, " Cannot access memory\n");
Signature · 1 line
2.19.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#5 lore raw

PATCH 3/3 elf_ops.h: Use address_space_write() to write memory

In reply to Peter Maydell

Currently the load_elf function in elf_ops.h uses cpu_physical_memory_write() to write the ELF file to memory if it is not handling it as a ROM blob. This means we ignore the AddressSpace that the function is passed to define where it should be loaded. Use address_space_write() instead.

---
 include/hw/elf_ops.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/hw/elf_ops.h +2 -1
index 81cecaf27e2..793dcb85c2b 100644
@@ -482,7 +482,8 @@ static int glue(load_elf, SZ)(const char *name, int fd,
                     rom_add_elf_program(label, data, file_size, mem_size,
                                         addr, as);
                 } else {
-                    cpu_physical_memory_write(addr, data, file_size);
+                    address_space_write(as, addr, MEMTXATTRS_UNSPECIFIED,
+                                        data, file_size);
                     g_free(data);
                 }
             }
Signature · 1 line
2.19.1
Reviewed-by on v1 3/3
#6 lore raw

Re: [Qemu-devel] [PATCH for-4.0 3/3] elf_ops.h: Use address_space_write() to write memory

Quoted 8 lines · Peter Maydell

Currently the load_elf function in elf_ops.h uses cpu_physical_memory_write() to write the ELF file to memory if it is not handling it as a ROM blob. This means we ignore the AddressSpace that the function is passed to define where it should be loaded. Use address_space_write() instead.

Quoted 19 lines
---
 include/hw/elf_ops.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/hw/elf_ops.h +2 -1
index 81cecaf27e2..793dcb85c2b 100644
@@ -482,7 +482,8 @@ static int glue(load_elf, SZ)(const char *name, int fd,
                     rom_add_elf_program(label, data, file_size, mem_size,
                                         addr, as);
                 } else {
-                    cpu_physical_memory_write(addr, data, file_size);
+                    address_space_write(as, addr, MEMTXATTRS_UNSPECIFIED,
+                                        data, file_size);
                     g_free(data);
                 }
             }
#7 lore raw

Re: [Qemu-devel] [PATCH for-4.0 3/3] elf_ops.h: Use address_space_write() to write memory

In reply to Peter Maydell
Quoted 17 lines · Peter Maydell

Currently the load_elf function in elf_ops.h uses cpu_physical_memory_write() to write the ELF file to memory if it is not handling it as a ROM blob. This means we ignore the AddressSpace that the function is passed to define where it should be loaded. Use address_space_write() instead.

---
 include/hw/elf_ops.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/hw/elf_ops.h +0 -0
index 81cecaf27e2..793dcb85c2b 100644

include/hw/elf_ops.h static int glue(load_elf, SZ)(const char *name, int fd,

Quoted 10 lines
include/hw/elf_ops.h +2 -2
@@ -482,7 +482,8 @@ static int glue(load_elf, SZ)(const char *name, int fd,
                     rom_add_elf_program(label, data, file_size, mem_size,
                                         addr, as);
                 } else {
-                    cpu_physical_memory_write(addr, data, file_size);
+                    address_space_write(as, addr, MEMTXATTRS_UNSPECIFIED,
+                                        data, file_size);
                     g_free(data);
                 }
             }
--

This turns out to have a bug which my testing somehow missed. The 'as' argument to this function can be NULL, which means that it should use address_space_memory, so we need to handle that. (The other side of the if() doesn't need to special case NULL because rom_add_elf_program() and the other loader.c code handle NULL later on.)

thanks
-- PMM