Skip to contentSkip to conversation

gdb invalid memory access handling improvements

PATCH Superseded Nicholas Piggin 4 participants 10 messages Mar 14, 2025 – Mar 17, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/2 gdbstub: Add phys_memory_rw_debug for physical memory access

In reply to Nicholas Piggin

Add an accessor for gdb physical memory access mode which sets the the .debug attribute for the MemTxAttribute, and also returns success to the caller.

GDB with PhyMemMode will now report failure from memory accesses outside valid system memory addresses, and it is also able to write to ROMs as GDB virtual memory access can.

---
 docs/devel/loads-stores.rst | 11 +++++++++++
 include/exec/cpu-common.h   |  3 +++
 gdbstub/system.c            |  7 +------
 system/physmem.c            | 16 ++++++++++++++++
 4 files changed, 31 insertions(+), 6 deletions(-)
docs/devel/loads-stores.rst +11 -0
index 9471bac8599..ac2e0d34d67 100644
@@ -481,6 +481,17 @@ would ignore the write attempt).
 
 ``cpu_memory_rw_debug``
 
+``phys_memory_rw_debug``
+~~~~~~~~~~~~~~~~~~~~~~~
+
+Access system memory by physical address for debug purposes.
+
+This function is intended for use by the GDB stub and similar code.
+It takes a physical address and operates on the system address space.
+Access is performed as in cpu_memory_rw_debug().
+
+``phys_memory_rw_debug``
+
 ``dma_memory_*``
 ~~~~~~~~~~~~~~~~
 
include/exec/cpu-common.h +3 -0
index 3771b2130c2..6429dc2331e 100644
@@ -181,6 +181,9 @@ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,
 /* Returns: 0 on success, -1 on error */
 int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
                         void *ptr, size_t len, bool is_write);
+/* Returns: 0 on success, -1 on error */
+int phys_memory_rw_debug(hwaddr addr, void *buf,
+                         hwaddr len, bool is_write);
 
 /* vl.c */
 void list_cpus(void);
gdbstub/system.c +1 -6
index dd22ff0fb3a..79fcb30f6f0 100644
@@ -457,12 +457,7 @@ int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
                                uint8_t *buf, int len, bool is_write)
 {
     if (phy_memory_mode) {
-        if (is_write) {
-            cpu_physical_memory_write(addr, buf, len);
-        } else {
-            cpu_physical_memory_read(addr, buf, len);
-        }
-        return 0;
+        return phys_memory_rw_debug(addr, buf, len, is_write);
     }
 
     if (cpu->cc->memory_rw_debug) {
system/physmem.c +16 -0
index e97de3ef65c..aa78b0d514b 100644
@@ -3743,6 +3743,22 @@ int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
     return 0;
 }
 
+/* physical memory access for debug (includes writing to ROM) */
+int phys_memory_rw_debug(hwaddr addr, void *buf,
+                         hwaddr len, bool is_write)
+{
+    MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
+    MemTxResult res;
+
+    attrs.debug = 1;
+    res = address_space_rw(&address_space_memory, addr, attrs,
+                           buf, len, is_write);
+    if (res != MEMTX_OK) {
+        return -1;
+    }
+    return 0;
+}
+
 bool cpu_physical_memory_is_io(hwaddr phys_addr)
 {
     MemoryRegion*mr;
Signature · 1 line
2.47.1
#0 lore raw

cover gdb invalid memory access handling improvements

This adds .debug=1 attribute for GDB's phys mem access mode, adds memory transaction error handling for it so it reports cannot access memory instead of silent success, and silences warning logs for invalid memory access coming from the debugger.

Thanks,
Nick

Nicholas Piggin (2):
gdbstub: Add phys_memory_rw_debug for physical memory access
memory: suppress INVALID_MEM logs caused by debug access

 docs/devel/loads-stores.rst | 11 +++++++++++
 include/exec/cpu-common.h   |  3 +++
 gdbstub/system.c            |  7 +------
 system/memory.c             | 37 ++++++++++++++++++++++---------------
 system/physmem.c            | 16 ++++++++++++++++
 5 files changed, 53 insertions(+), 21 deletions(-)
Signature · 1 line
2.47.1
#4 lore raw

PATCH 2/2 memory: suppress INVALID_MEM logs caused by debug access

Debugger-driven invalid memory accesses are not guest errors, so should not cause these error logs.

Debuggers can access memory wildly, including access to addresses not specified by the user (e.g., gdb it might try to walk the stack or load target addresses to display disassembly). Failure is reported synchronously by the GDB protcol so the user can be notified via the debugger client.

---
 system/memory.c | 37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)
system/memory.c +22 -15
index 4c829793a0a..960f66e8d7e 100644
@@ -1412,18 +1412,23 @@ bool memory_region_access_valid(MemoryRegion *mr,
 {
     if (mr->ops->valid.accepts
         && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write, attrs)) {
-        qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
-                      ", size %u, region '%s', reason: rejected\n",
-                      is_write ? "write" : "read",
-                      addr, size, memory_region_name(mr));
+        if (attrs.debug) {
+            /* Don't log memory errors due to debugger accesses */
+            qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
+                          ", size %u, region '%s', reason: rejected\n",
+                          is_write ? "write" : "read",
+                          addr, size, memory_region_name(mr));
+        }
         return false;
     }
 
     if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
-        qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
-                      ", size %u, region '%s', reason: unaligned\n",
-                      is_write ? "write" : "read",
-                      addr, size, memory_region_name(mr));
+        if (attrs.debug) {
+            qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
+                          ", size %u, region '%s', reason: unaligned\n",
+                          is_write ? "write" : "read",
+                          addr, size, memory_region_name(mr));
+        }
         return false;
     }
 
@@ -1434,13 +1439,15 @@ bool memory_region_access_valid(MemoryRegion *mr,
 
     if (size > mr->ops->valid.max_access_size
         || size < mr->ops->valid.min_access_size) {
-        qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
-                      ", size %u, region '%s', reason: invalid size "
-                      "(min:%u max:%u)\n",
-                      is_write ? "write" : "read",
-                      addr, size, memory_region_name(mr),
-                      mr->ops->valid.min_access_size,
-                      mr->ops->valid.max_access_size);
+        if (attrs.debug) {
+            qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
+                          ", size %u, region '%s', reason: invalid size "
+                          "(min:%u max:%u)\n",
+                          is_write ? "write" : "read",
+                          addr, size, memory_region_name(mr),
+                          mr->ops->valid.min_access_size,
+                          mr->ops->valid.max_access_size);
+        }
         return false;
     }
     return true;
Signature · 1 line
2.47.1
#5 lore raw

Re: [PATCH 2/2] memory: suppress INVALID_MEM logs caused by debug access

Quoted 19 lines · Nicholas Piggin

Debugger-driven invalid memory accesses are not guest errors, so should not cause these error logs.

Debuggers can access memory wildly, including access to addresses not specified by the user (e.g., gdb it might try to walk the stack or load target addresses to display disassembly). Failure is reported synchronously by the GDB protcol so the user can be notified via the debugger client.

---
  system/memory.c | 37 ++++++++++++++++++++++---------------
  1 file changed, 22 insertions(+), 15 deletions(-)
system/memory.c +0 -0
index 4c829793a0a..960f66e8d7e 100644

system/memory.c bool memory_region_access_valid(MemoryRegion *mr, · line 1412

Quoted 1 line
system/memory.c +0 -0
@@ -1412,18 +1412,23 @@ bool memory_region_access_valid(MemoryRegion *mr,
  {

Should we instead consider debug accesses as always valid? i.e.:

        if (attrs.debug) {
            return true;
        }
Quoted 53 lines
system/memory.c +22 -15
      if (mr->ops->valid.accepts
          && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write, attrs)) {
-        qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
-                      ", size %u, region '%s', reason: rejected\n",
-                      is_write ? "write" : "read",
-                      addr, size, memory_region_name(mr));
+        if (attrs.debug) {
+            /* Don't log memory errors due to debugger accesses */
+            qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
+                          ", size %u, region '%s', reason: rejected\n",
+                          is_write ? "write" : "read",
+                          addr, size, memory_region_name(mr));
+        }
          return false;
      }
 
      if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
-        qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
-                      ", size %u, region '%s', reason: unaligned\n",
-                      is_write ? "write" : "read",
-                      addr, size, memory_region_name(mr));
+        if (attrs.debug) {
+            qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
+                          ", size %u, region '%s', reason: unaligned\n",
+                          is_write ? "write" : "read",
+                          addr, size, memory_region_name(mr));
+        }
          return false;
      }
 
@@ -1434,13 +1439,15 @@ bool memory_region_access_valid(MemoryRegion *mr,
 
      if (size > mr->ops->valid.max_access_size
          || size < mr->ops->valid.min_access_size) {
-        qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
-                      ", size %u, region '%s', reason: invalid size "
-                      "(min:%u max:%u)\n",
-                      is_write ? "write" : "read",
-                      addr, size, memory_region_name(mr),
-                      mr->ops->valid.min_access_size,
-                      mr->ops->valid.max_access_size);
+        if (attrs.debug) {
+            qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
+                          ", size %u, region '%s', reason: invalid size "
+                          "(min:%u max:%u)\n",
+                          is_write ? "write" : "read",
+                          addr, size, memory_region_name(mr),
+                          mr->ops->valid.min_access_size,
+                          mr->ops->valid.max_access_size);
+        }
          return false;
      }
      return true;
#2 lore raw

Re: [PATCH 1/2] gdbstub: Add phys_memory_rw_debug for physical memory access

In reply to Nicholas Piggin
Quoted 8 lines · Nicholas Piggin

Add an accessor for gdb physical memory access mode which sets the the .debug attribute for the MemTxAttribute, and also returns success to the caller.

GDB with PhyMemMode will now report failure from memory accesses outside valid system memory addresses, and it is also able to write to ROMs as GDB virtual memory access can.

Quoted 8 lines
---
  docs/devel/loads-stores.rst | 11 +++++++++++
  include/exec/cpu-common.h   |  3 +++
  gdbstub/system.c            |  7 +------
  system/physmem.c            | 16 ++++++++++++++++
  4 files changed, 31 insertions(+), 6 deletions(-)

I think you might as well put this function in gdbstub/system.c and not export (or document) it.

r~

#6 lore raw

Re: [PATCH 2/2] memory: suppress INVALID_MEM logs caused by debug access

In reply to Philippe Mathieu-Daudé
Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Nicholas Piggin

Debugger-driven invalid memory accesses are not guest errors, so should not cause these error logs.

Debuggers can access memory wildly, including access to addresses not specified by the user (e.g., gdb it might try to walk the stack or load target addresses to display disassembly). Failure is reported synchronously by the GDB protcol so the user can be notified via the debugger client.

---
  system/memory.c | 37 ++++++++++++++++++++++---------------

  1 file changed, 22 insertions(+), 15 deletions(-)
system/memory.c +0 -0
index 4c829793a0a..960f66e8d7e 100644

system/memory.c bool memory_region_access_valid(MemoryRegion *mr, · line 1412

Quoted 1 line
system/memory.c +0 -0
@@ -1412,18 +1412,23 @@ bool memory_region_access_valid(MemoryRegion *mr,
   {

Should we instead consider debug accesses as always valid? i.e.:

Quoted 3 lines

        if (attrs.debug) {
            return true;
        }

No. You're likely to hit assertions in the device code.

r~

Reviewed-by on the v1 series
#9 lore raw
In reply to Nicholas Piggin
Quoted 4 lines · Nicholas Piggin

This adds .debug=1 attribute for GDB's phys mem access mode, adds memory transaction error handling for it so it reports cannot access memory instead of silent success, and silences warning logs for invalid memory access coming from the debugger.

Nothing jumped at me, with Richard's suggestion of keeping the new function limited in scope

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw

Re: [PATCH 1/2] gdbstub: Add phys_memory_rw_debug for physical memory access

In reply to Richard Henderson
Quoted 18 lines · Richard Henderson
Quoted 8 lines · Nicholas Piggin

Add an accessor for gdb physical memory access mode which sets the the .debug attribute for the MemTxAttribute, and also returns success to the caller.

GDB with PhyMemMode will now report failure from memory accesses outside valid system memory addresses, and it is also able to write to ROMs as GDB virtual memory access can.

Quoted 8 lines
---
  docs/devel/loads-stores.rst | 11 +++++++++++
  include/exec/cpu-common.h   |  3 +++
  gdbstub/system.c            |  7 +------
  system/physmem.c            | 16 ++++++++++++++++
  4 files changed, 31 insertions(+), 6 deletions(-)
Quoted 2 lines

I think you might as well put this function in gdbstub/system.c and not export (or document) it.

A possible advantage this way is gdbstub not knowing precise details of the memory transaction (i.e., .debug = 1), but I can do that. Will submit a v2.

Thanks,
Nick

#7 lore raw

Re: [PATCH 2/2] memory: suppress INVALID_MEM logs caused by debug access

In reply to Nicholas Piggin
Quoted 19 lines · Nicholas Piggin

Debugger-driven invalid memory accesses are not guest errors, so should not cause these error logs.

Debuggers can access memory wildly, including access to addresses not specified by the user (e.g., gdb it might try to walk the stack or load target addresses to display disassembly). Failure is reported synchronously by the GDB protcol so the user can be notified via the debugger client.

---
  system/memory.c | 37 ++++++++++++++++++++++---------------
  1 file changed, 22 insertions(+), 15 deletions(-)
system/memory.c +0 -0
index 4c829793a0a..960f66e8d7e 100644

system/memory.c bool memory_region_access_valid(MemoryRegion *mr, · line 1412

Quoted 1 line
system/memory.c +0 -0
@@ -1412,18 +1412,23 @@ bool memory_region_access_valid(MemoryRegion *mr,
  {

Alternatively:

        int invalid_mem_mask = attrs.debug ? LOG_INVALID_MEM : 0;
Quoted 15 lines
system/memory.c +7 -4
      if (mr->ops->valid.accepts
          && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write, attrs)) {
-        qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
-                      ", size %u, region '%s', reason: rejected\n",
-                      is_write ? "write" : "read",
-                      addr, size, memory_region_name(mr));
+        if (attrs.debug) {
+            /* Don't log memory errors due to debugger accesses */
+            qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
+                          ", size %u, region '%s', reason: rejected\n",
+                          is_write ? "write" : "read",
+                          addr, size, memory_region_name(mr));
+        }
          return false;
      }
#8 lore raw

Re: [PATCH 2/2] memory: suppress INVALID_MEM logs caused by debug access

Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Nicholas Piggin

Debugger-driven invalid memory accesses are not guest errors, so should not cause these error logs.

Debuggers can access memory wildly, including access to addresses not specified by the user (e.g., gdb it might try to walk the stack or load target addresses to display disassembly). Failure is reported synchronously by the GDB protcol so the user can be notified via the debugger client.

---
  system/memory.c | 37 ++++++++++++++++++++++---------------
  1 file changed, 22 insertions(+), 15 deletions(-)
system/memory.c +0 -0
index 4c829793a0a..960f66e8d7e 100644

system/memory.c bool memory_region_access_valid(MemoryRegion *mr, · line 1412

Quoted 1 line
system/memory.c +0 -0
@@ -1412,18 +1412,23 @@ bool memory_region_access_valid(MemoryRegion *mr,
  {

Alternatively:

Quoted 1 line
         int invalid_mem_mask = attrs.debug ? LOG_INVALID_MEM : 0;

Oh that's a thing? Would save a level of indent and might look nicer. (I guess you have x : y expressions reversed)

Thanks,
Nick

Quoted 16 lines
Quoted 16 lines
system/memory.c +7 -4
      if (mr->ops->valid.accepts
          && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write, attrs)) {
-        qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
-                      ", size %u, region '%s', reason: rejected\n",
-                      is_write ? "write" : "read",
-                      addr, size, memory_region_name(mr));
+        if (attrs.debug) {
+            /* Don't log memory errors due to debugger accesses */
+            qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
+                          ", size %u, region '%s', reason: rejected\n",
+                          is_write ? "write" : "read",
+                          addr, size, memory_region_name(mr));
+        }
          return false;
      }