[RFC PATCH 08/16] system/physmem: Add helper to check whether a guest PA maps to RAM

Shameer Kolothum posted 16 patches 2 months ago
[RFC PATCH 08/16] system/physmem: Add helper to check whether a guest PA maps to RAM
Posted by Shameer Kolothum 2 months ago
From: Nicolin Chen <nicolinc@nvidia.com>

Introduce cpu_physical_memory_is_ram(), a helper that performs an
address_space translation and returns whether the resolved MemoryRegion is
backed by RAM.

This will be used by the upcoming Tegra241 CMDQV support to validate
guest provided VCMDQ buffer addresses.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Shameer Kolothum <skolothumtho@nvidia.com>
---
 include/exec/cpu-common.h |  2 ++
 system/physmem.c          | 12 ++++++++++++
 2 files changed, 14 insertions(+)

diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h
index e0be4ee2b8..76b91d1b9b 100644
--- a/include/exec/cpu-common.h
+++ b/include/exec/cpu-common.h
@@ -148,6 +148,8 @@ void qemu_flush_coalesced_mmio_buffer(void);
 
 typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
 
+bool cpu_physical_memory_is_ram(hwaddr phys_addr);
+
 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque);
 
 /* vl.c */
diff --git a/system/physmem.c b/system/physmem.c
index c9869e4049..1f6c821a0e 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -4068,6 +4068,18 @@ int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
     return 0;
 }
 
+bool cpu_physical_memory_is_ram(hwaddr phys_addr)
+{
+    MemoryRegion *mr;
+    hwaddr l = 1;
+
+    RCU_READ_LOCK_GUARD();
+    mr = address_space_translate(&address_space_memory, phys_addr, &phys_addr,
+                                 &l, false, MEMTXATTRS_UNSPECIFIED);
+
+    return memory_region_is_ram(mr);
+}
+
 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
 {
     RAMBlock *block;
-- 
2.43.0
Re: [RFC PATCH 08/16] system/physmem: Add helper to check whether a guest PA maps to RAM
Posted by Eric Auger 4 weeks ago

On 12/10/25 2:37 PM, Shameer Kolothum wrote:
> From: Nicolin Chen <nicolinc@nvidia.com>
>
> Introduce cpu_physical_memory_is_ram(), a helper that performs an
> address_space translation and returns whether the resolved MemoryRegion is
> backed by RAM.
>
> This will be used by the upcoming Tegra241 CMDQV support to validate
> guest provided VCMDQ buffer addresses.
>
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> Signed-off-by: Shameer Kolothum <skolothumtho@nvidia.com>
> ---
>  include/exec/cpu-common.h |  2 ++
>  system/physmem.c          | 12 ++++++++++++
>  2 files changed, 14 insertions(+)
>
> diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h
> index e0be4ee2b8..76b91d1b9b 100644
> --- a/include/exec/cpu-common.h
> +++ b/include/exec/cpu-common.h
> @@ -148,6 +148,8 @@ void qemu_flush_coalesced_mmio_buffer(void);
>  
>  typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
>  
> +bool cpu_physical_memory_is_ram(hwaddr phys_addr);
> +
>  int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque);
>  
>  /* vl.c */
> diff --git a/system/physmem.c b/system/physmem.c
> index c9869e4049..1f6c821a0e 100644
> --- a/system/physmem.c
> +++ b/system/physmem.c
> @@ -4068,6 +4068,18 @@ int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
>      return 0;
>  }
>  
> +bool cpu_physical_memory_is_ram(hwaddr phys_addr)
I guess this would need to match the new address_space_is_io()

Eric
> +{
> +    MemoryRegion *mr;
> +    hwaddr l = 1;
> +
> +    RCU_READ_LOCK_GUARD();
> +    mr = address_space_translate(&address_space_memory, phys_addr, &phys_addr,
> +                                 &l, false, MEMTXATTRS_UNSPECIFIED);
> +
> +    return memory_region_is_ram(mr);
> +}
> +
>  int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
>  {
>      RAMBlock *block;