[RFC PATCH v2 094/137] hw/i386: Give memory regions an explicit owner

Alexander Graf <[email protected]>
Newsgroups org.nongnu.qemu-arm,org.nongnu.qemu-devel,org.nongnu.qemu-riscv
Message-ID <[email protected]>
Convert memory_region_init*() calls that pass NULL owner to pass
the enclosing PC/x86/microvm machine or device instead.

Thread X86MachineState through x86_isa_bios_init() so the isa-bios
alias is owned by the machine that creates it.

No functional change intended.

RAMBlock idstrs are unchanged: the owners added here are Machine
or SysBus devices, and memory_region_register_ram() maps both to
dev=NULL for qemu_ram_set_idstr().

AI-used-for: code (refactoring)
Signed-off-by: Alexander Graf <[email protected]>
---
 hw/i386/microvm.c          |  4 ++--
 hw/i386/pc.c               |  8 ++++----
 hw/i386/pc_piix.c          |  2 +-
 hw/i386/pc_q35.c           |  2 +-
 hw/i386/pc_sysfw.c         |  6 +++---
 hw/i386/x86-common.c       | 11 ++++++-----
 hw/i386/xen/xen-hvm.c      |  8 ++++----
 hw/i386/xen/xen_pvdevice.c |  2 +-
 include/hw/i386/x86.h      |  3 ++-
 9 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c
index aa0e1ef155..751e3962fa 100644
--- a/hw/i386/microvm.c
+++ b/hw/i386/microvm.c
@@ -305,7 +305,7 @@ static void microvm_memory_init(MicrovmMachineState *mms)
     }
 
     ram_below_4g = g_malloc(sizeof(*ram_below_4g));
-    memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", machine->ram,
+    memory_region_init_alias(ram_below_4g, OBJECT(mms), "ram-below-4g", machine->ram,
                              0, x86ms->below_4g_mem_size);
     memory_region_add_subregion(system_memory, 0, ram_below_4g);
 
@@ -313,7 +313,7 @@ static void microvm_memory_init(MicrovmMachineState *mms)
 
     if (x86ms->above_4g_mem_size > 0) {
         ram_above_4g = g_malloc(sizeof(*ram_above_4g));
-        memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g",
+        memory_region_init_alias(ram_above_4g, OBJECT(mms), "ram-above-4g",
                                  machine->ram,
                                  x86ms->below_4g_mem_size,
                                  x86ms->above_4g_mem_size);
diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index e505391985..8057058ff0 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -795,13 +795,13 @@ void pc_memory_init(PCMachineState *pcms,
      * done for backwards compatibility with older qemus.
      */
     ram_below_4g = g_malloc(sizeof(*ram_below_4g));
-    memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", machine->ram,
+    memory_region_init_alias(ram_below_4g, OBJECT(pcms), "ram-below-4g", machine->ram,
                              0, x86ms->below_4g_mem_size);
     memory_region_add_subregion(system_memory, 0, ram_below_4g);
     e820_add_entry(0, x86ms->below_4g_mem_size, E820_RAM);
     if (x86ms->above_4g_mem_size > 0) {
         ram_above_4g = g_malloc(sizeof(*ram_above_4g));
-        memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g",
+        memory_region_init_alias(ram_above_4g, OBJECT(pcms), "ram-above-4g",
                                  machine->ram,
                                  x86ms->below_4g_mem_size,
                                  x86ms->above_4g_mem_size);
@@ -871,10 +871,10 @@ void pc_memory_init(PCMachineState *pcms,
     if (!is_tdx_vm()) {
         option_rom_mr = g_malloc(sizeof(*option_rom_mr));
         if (machine_require_guest_memfd(machine)) {
-            memory_region_init_ram_guest_memfd(option_rom_mr, NULL, "pc.rom",
+            memory_region_init_ram_guest_memfd(option_rom_mr, OBJECT(pcms), "pc.rom",
                                             PC_ROM_SIZE, &error_fatal);
         } else {
-            memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
+            memory_region_init_ram(option_rom_mr, OBJECT(pcms), "pc.rom", PC_ROM_SIZE,
                                 &error_fatal);
             if (pcmc->pci_enabled) {
                 memory_region_set_readonly(option_rom_mr, true);
diff --git a/hw/i386/pc_piix.c b/hw/i386/pc_piix.c
index c9e6c61734..0b8d7d88b9 100644
--- a/hw/i386/pc_piix.c
+++ b/hw/i386/pc_piix.c
@@ -191,7 +191,7 @@ static void pc_init1(MachineState *machine, const char *pci_type)
     }
 
     pci_memory = g_new(MemoryRegion, 1);
-    memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
+    memory_region_init(pci_memory, OBJECT(machine), "pci", UINT64_MAX);
 
     phb = OBJECT(qdev_new(OBJECT(machine), "i440fx", TYPE_I440FX_PCI_HOST_BRIDGE));
     object_property_set_link(phb, PCI_HOST_PROP_RAM_MEM,
diff --git a/hw/i386/pc_q35.c b/hw/i386/pc_q35.c
index 62625abed8..b2cdca8ade 100644
--- a/hw/i386/pc_q35.c
+++ b/hw/i386/pc_q35.c
@@ -208,7 +208,7 @@ static void pc_q35_init(MachineState *machine)
                                                &error_abort);
 
     /* allocate ram and load rom/bios */
-    memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
+    memory_region_init(pci_memory, OBJECT(machine), "pci", UINT64_MAX);
     pc_memory_init(pcms, system_memory, pci_memory, pci_hole64_size);
 
     object_property_set_link(phb, PCI_HOST_PROP_RAM_MEM,
diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
index 02d75e2838..b536fca60f 100644
--- a/hw/i386/pc_sysfw.c
+++ b/hw/i386/pc_sysfw.c
@@ -52,10 +52,10 @@ static void pc_isa_bios_init(PCMachineState *pcms, MemoryRegion *isa_bios,
     /* map the last 128KB of the BIOS in ISA space */
     isa_bios_size = MIN(flash_size, 128 * KiB);
     if (machine_require_guest_memfd(MACHINE(pcms))) {
-        memory_region_init_ram_guest_memfd(isa_bios, NULL, "isa-bios",
+        memory_region_init_ram_guest_memfd(isa_bios, OBJECT(pcms), "isa-bios",
                                            isa_bios_size, &error_fatal);
     } else {
-        memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
+        memory_region_init_ram(isa_bios, OBJECT(pcms), "isa-bios", isa_bios_size,
                                &error_fatal);
     }
     memory_region_add_subregion_overlap(rom_memory,
@@ -189,7 +189,7 @@ static void pc_system_flash_map(PCMachineState *pcms,
         if (i == 0) {
             flash_mem = pflash_cfi01_get_memory(system_flash);
             if (pcmc->isa_bios_alias) {
-                x86_isa_bios_init(&x86ms->isa_bios, rom_memory, flash_mem,
+                x86_isa_bios_init(x86ms, &x86ms->isa_bios, rom_memory, flash_mem,
                                   true);
             } else {
                 pc_isa_bios_init(pcms, &x86ms->isa_bios, rom_memory, flash_mem);
diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c
index 73a53fbf3f..151a67ee5f 100644
--- a/hw/i386/x86-common.c
+++ b/hw/i386/x86-common.c
@@ -993,13 +993,14 @@ void x86_load_linux(X86MachineState *x86ms,
     nb_option_roms++;
 }
 
-void x86_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *isa_memory,
+void x86_isa_bios_init(X86MachineState *x86ms, MemoryRegion *isa_bios,
+                       MemoryRegion *isa_memory,
                        MemoryRegion *bios, bool read_only)
 {
     uint64_t bios_size = memory_region_size(bios);
     uint64_t isa_bios_size = MIN(bios_size, 128 * KiB);
 
-    memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
+    memory_region_init_alias(isa_bios, OBJECT(x86ms), "isa-bios", bios,
                              bios_size - isa_bios_size, isa_bios_size);
     memory_region_add_subregion_overlap(isa_memory, 1 * MiB - isa_bios_size,
                                         isa_bios, 1);
@@ -1036,13 +1037,13 @@ static void load_bios_from_file(X86MachineState *x86ms, const char *bios_name,
 
     /* BIOS load */
     if (machine_require_guest_memfd(MACHINE(x86ms))) {
-        memory_region_init_ram_guest_memfd(&x86ms->bios, NULL, "pc.bios",
+        memory_region_init_ram_guest_memfd(&x86ms->bios, OBJECT(x86ms), "pc.bios",
                                            bios_size, &error_fatal);
         if (is_tdx_vm()) {
             tdx_set_tdvf_region(&x86ms->bios);
         }
     } else {
-        memory_region_init_ram(&x86ms->bios, NULL, "pc.bios",
+        memory_region_init_ram(&x86ms->bios, OBJECT(x86ms), "pc.bios",
                                bios_size, &error_fatal);
     }
     if (sev_enabled() || is_tdx_vm()) {
@@ -1107,7 +1108,7 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
 
     if (!machine_require_guest_memfd(MACHINE(x86ms))) {
         /* map the last 128KB of the BIOS in ISA space */
-        x86_isa_bios_init(&x86ms->isa_bios, rom_memory, &x86ms->bios,
+        x86_isa_bios_init(x86ms, &x86ms->isa_bios, rom_memory, &x86ms->bios,
                           !isapc_ram_fw);
     }
 
diff --git a/hw/i386/xen/xen-hvm.c b/hw/i386/xen/xen-hvm.c
index d3ce082e07..01d851ce3e 100644
--- a/hw/i386/xen/xen-hvm.c
+++ b/hw/i386/xen/xen-hvm.c
@@ -156,11 +156,11 @@ static void xen_ram_init(PCMachineState *pcms,
          */
         block_len = (4 * GiB) + x86ms->above_4g_mem_size;
     }
-    memory_region_init_ram(&xen_memory, NULL, "xen.ram", block_len,
+    memory_region_init_ram(&xen_memory, OBJECT(pcms), "xen.ram", block_len,
                            &error_fatal);
     *ram_memory_p = &xen_memory;
 
-    memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
+    memory_region_init_alias(&ram_640k, OBJECT(pcms), "xen.ram.640k",
                              &xen_memory, 0, 0xa0000);
     memory_region_add_subregion(sysmem, 0, &ram_640k);
     /* Skip of the VGA IO memory space, it will be registered later by the VGA
@@ -169,12 +169,12 @@ static void xen_ram_init(PCMachineState *pcms,
      * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
      * the Options ROM, so it is registered here as RAM.
      */
-    memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
+    memory_region_init_alias(&ram_lo, OBJECT(pcms), "xen.ram.lo",
                              &xen_memory, 0xc0000,
                              x86ms->below_4g_mem_size - 0xc0000);
     memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
     if (x86ms->above_4g_mem_size > 0) {
-        memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
+        memory_region_init_alias(&ram_hi, OBJECT(pcms), "xen.ram.hi",
                                  &xen_memory, 0x100000000ULL,
                                  x86ms->above_4g_mem_size);
         memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
diff --git a/hw/i386/xen/xen_pvdevice.c b/hw/i386/xen/xen_pvdevice.c
index fab26a06af..6c255aeae1 100644
--- a/hw/i386/xen/xen_pvdevice.c
+++ b/hw/i386/xen/xen_pvdevice.c
@@ -108,7 +108,7 @@ static void xen_pv_realize(PCIDevice *pci_dev, Error **errp)
 
     pci_conf[PCI_INTERRUPT_PIN] = 1;
 
-    memory_region_init_io(&d->mmio, NULL, &xen_pv_mmio_ops, d,
+    memory_region_init_io(&d->mmio, OBJECT(pci_dev), &xen_pv_mmio_ops, d,
                           "mmio", d->size);
 
     pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h
index 61d6ec1755..9d5d7694d4 100644
--- a/include/hw/i386/x86.h
+++ b/include/hw/i386/x86.h
@@ -121,7 +121,8 @@ void x86_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
 void x86_cpu_unplug_cb(HotplugHandler *hotplug_dev,
                        DeviceState *dev, Error **errp);
 
-void x86_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *isa_memory,
+void x86_isa_bios_init(X86MachineState *x86ms, MemoryRegion *isa_bios,
+                       MemoryRegion *isa_memory,
                        MemoryRegion *bios, bool read_only);
 void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
                        MemoryRegion *rom_memory, bool isapc_ram_fw);
-- 
2.47.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.