Skip to contentSkip to conversation

hw: Sanitize various MemoryRegion uses

PATCH Superseded Philippe Mathieu-Daudé 2 participants 13 messages Feb 24, 2020 – Feb 25, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/32 memory: Correctly return alias region type

In reply to Philippe Mathieu-Daudé

Since memory region aliases are neither rom nor ram, they are described as i/o, which is often incorrect. Return instead the type of the original region we are aliasing.

---
Cc: [email protected]

 memory.c | 3 +++
 1 file changed, 3 insertions(+)
memory.c +3 -0
index aeaa8dcc9e..ce1179874e 100644
@@ -2818,6 +2818,9 @@ void address_space_destroy(AddressSpace *as)
 
 static const char *memory_region_type(MemoryRegion *mr)
 {
+    if (mr->alias) {
+        return memory_region_type(mr->alias);
+    }
     if (memory_region_is_ram_device(mr)) {
         return "ramd";
     } else if (memory_region_is_romd(mr)) {
Signature · 1 line
2.21.1
#3 lore raw

PATCH v2 2/32 memory: Simplify memory_region_init_rom_nomigrate() to ease review

In reply to Philippe Mathieu-Daudé

memory_region_init_rom_nomigrate() has the same content than memory_region_init_ram_shared_nomigrate(), with setting the readonly mode. The code is easier to review as creating a readonly ram/shared/nomigrate region.

---
 memory.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
memory.c +1 -12
index ce1179874e..6a327a54ed 100644
@@ -1659,19 +1659,8 @@ void memory_region_init_rom_nomigrate(MemoryRegion *mr,
                                       uint64_t size,
                                       Error **errp)
 {
-    Error *err = NULL;
-    memory_region_init(mr, owner, name, size);
-    mr->ram = true;
+    memory_region_init_ram_shared_nomigrate(mr, owner, name, size, false, errp);
     mr->readonly = true;
-    mr->terminates = true;
-    mr->destructor = memory_region_destructor_ram;
-    mr->ram_block = qemu_ram_alloc(size, false, mr, &err);
-    mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
-    if (err) {
-        mr->size = int128_zero();
-        object_unparent(OBJECT(mr));
-        error_propagate(errp, err);
-    }
 }
 
 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
Signature · 1 line
2.21.1
#0 lore raw

v2 cover hw: Sanitize various MemoryRegion uses

This series simplifies various memory API calls when creating memory regions.

Most of the patches are generated with Coccinelle semantic patches (provided).
Few more cleanups added while writting the patches have been added.

v1 was 'Let devices own the MemoryRegion they create':
https://www.mail-archive.com/[email protected]/msg681960.html

Since v1:
- understood a bit more Peter Maydell comments regarding how memory devices are migrated.

Supersedes: <[email protected]>

Philippe Mathieu-Daudé (32):
memory: Correctly return alias region type
memory: Simplify memory_region_init_rom_nomigrate() to ease review scripts/cocci: Rename memory-region-{init-ram -> housekeeping} scripts/cocci: Patch to replace memory_region_init_{ram,readonly ->

    rom}

hw/arm: Use memory_region_init_rom() with read-only regions hw/display: Use memory_region_init_rom() with read-only regions hw/mips: Use memory_region_init_rom() with read-only regions hw/m68k: Use memory_region_init_rom() with read-only regions hw/net: Use memory_region_init_rom() with read-only regions hw/pci-host: Use memory_region_init_rom() with read-only regions hw/ppc: Use memory_region_init_rom() with read-only regions hw/riscv: Use memory_region_init_rom() with read-only regions hw/sh4: Use memory_region_init_rom() with read-only regions hw/sparc: Use memory_region_init_rom() with read-only regions scripts/cocci: Patch to detect potential use of memory_region_init_rom hw/arm/stm32: Use memory_region_init_rom() with read-only regions hw/ppc/ppc405: Use memory_region_init_rom() with read-only regions hw/i386/pc_sysfw: Simplify using memory_region_init_alias() hw/i386/pc_sysfw: Remove unused 'ram_size' argument scripts/cocci: Patch to remove unnecessary
memory_region_set_readonly()
hw/arm: Remove unnecessary memory_region_set_readonly() on ROM alias scripts/cocci: Patch to let devices own their MemoryRegions hw/arm: Let devices own the MemoryRegion they create hw/char: Let devices own the MemoryRegion they create hw/core: Let devices own the MemoryRegion they create hw/display: Let devices own the MemoryRegion they create hw/dma: Let devices own the MemoryRegion they create hw/riscv: Let devices own the MemoryRegion they create hw/input/milkymist-softusb: Remove unused 'pmem_ptr' field hw/input/milkymist-softusb: Let devices own the MemoryRegion they
create
hw/net/milkymist-minimac2: Let devices own the MemoryRegion they
create
hw/block/onenand: Let devices own the MemoryRegion they create

 .../memory-region-housekeeping.cocci          | 159 ++++++++++++++++++
 .../coccinelle/memory-region-init-ram.cocci   |  38 -----
 hw/arm/exynos4210.c                           |  14 +-
 hw/arm/fsl-imx25.c                            |  10 +-
 hw/arm/fsl-imx31.c                            |   6 +-
 hw/arm/fsl-imx6.c                             |   6 +-
 hw/arm/fsl-imx6ul.c                           |   9 +-
 hw/arm/mainstone.c                            |   3 +-
 hw/arm/msf2-soc.c                             |   6 +-
 hw/arm/nrf51_soc.c                            |   2 +-
 hw/arm/omap_sx1.c                             |   6 +-
 hw/arm/palm.c                                 |   3 +-
 hw/arm/spitz.c                                |   3 +-
 hw/arm/stellaris.c                            |   3 +-
 hw/arm/stm32f205_soc.c                        |  11 +-
 hw/arm/stm32f405_soc.c                        |  12 +-
 hw/arm/tosa.c                                 |   3 +-
 hw/arm/xlnx-zynqmp.c                          |  11 +-
 hw/block/onenand.c                            |   7 +-
 hw/char/serial.c                              |   7 +-
 hw/core/platform-bus.c                        |   3 +-
 hw/display/cg3.c                              |   5 +-
 hw/display/g364fb.c                           |   3 +-
 hw/display/macfb.c                            |   4 +-
 hw/display/tcx.c                              |   5 +-
 hw/dma/i8257.c                                |   2 +-
 hw/dma/rc4030.c                               |   4 +-
 hw/i386/pc_sysfw.c                            |  29 +---
 hw/input/milkymist-softusb.c                  |  12 +-
 hw/m68k/q800.c                                |   3 +-
 hw/mips/mips_fulong2e.c                       |   3 +-
 hw/mips/mips_jazz.c                           |   6 +-
 hw/mips/mips_mipssim.c                        |   3 +-
 hw/mips/mips_r4k.c                            |   3 +-
 hw/net/dp8393x.c                              |   5 +-
 hw/net/milkymist-minimac2.c                   |   8 +-
 hw/pci-host/prep.c                            |   5 +-
 hw/ppc/mac_newworld.c                         |   3 +-
 hw/ppc/mac_oldworld.c                         |   3 +-
 hw/ppc/ppc405_boards.c                        |   6 +-
 hw/riscv/sifive_e.c                           |   9 +-
 hw/riscv/sifive_u.c                           |   2 +-
 hw/sh4/shix.c                                 |   3 +-
 hw/sparc/leon3.c                              |   3 +-
 memory.c                                      |  16 +-
 MAINTAINERS                                   |   1 +
 46 files changed, 268 insertions(+), 200 deletions(-)
 create mode 100644 scripts/coccinelle/memory-region-housekeeping.cocci
 delete mode 100644 scripts/coccinelle/memory-region-init-ram.cocci
Signature · 1 line
2.21.1
#4 lore raw

PATCH v2 3/32 scripts/cocci: Rename memory-region-{init-ram -> housekeeping}

As we are going to add various semantic changes related to the memory region API, rename this script to be more generic.
Add a 'usage' header, and an entry in MAINTAINERS to avoid checkpatch warning.

---
 ...t-ram.cocci => memory-region-housekeeping.cocci} | 13 +++++++++++++
 MAINTAINERS                                         |  1 +
 2 files changed, 14 insertions(+)
 rename scripts/coccinelle/{memory-region-init-ram.cocci => memory-region-housekeeping.cocci} (84%)
scripts/coccinelle/memory-region-housekeeping.cocci renamed from scripts/coccinelle/memory-region-init-ram.cocci +13 -0
similarity index 84%
rename from scripts/coccinelle/memory-region-init-ram.cocci
rename to scripts/coccinelle/memory-region-housekeeping.cocci
index d290150872..3699c1017e 100644
@@ -1,3 +1,16 @@
+/*
+  Usage:
+
+    spatch \
+        --macro-file scripts/cocci-macro-file.h \
+        --sp-file scripts/coccinelle/memory-region-housekeeping.cocci \
+        --keep-comments \
+        --in-place \
+        --dir .
+
+*/
+
+
 // Replace by-hand memory_region_init_ram_nomigrate/vmstate_register_ram
 // code sequences with use of the new memory_region_init_ram function.
 // Similarly for the _rom and _rom_device functions.
MAINTAINERS +1 -0
index 195dd58cac..316a8edbb6 100644
@@ -1998,6 +1998,7 @@ F: include/exec/ramblock.h
 F: memory.c
 F: include/exec/memory-internal.h
 F: exec.c
+F: scripts/coccinelle/memory-region-housekeeping.cocci
 
 SPICE
 M: Gerd Hoffmann <[email protected]>
Signature · 1 line
2.21.1
#5 lore raw

PATCH v2 4/32 scripts/cocci: Patch to replace memory_region_init_{ram, readonly -> rom}

In reply to Philippe Mathieu-Daudé

Add a semantic patch to replace memory_region_init_ram(readonly) by memory_region_init_rom().

---
 .../memory-region-housekeeping.cocci           | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
scripts/coccinelle/memory-region-housekeeping.cocci +18 -0
index 3699c1017e..ee3923d369 100644
@@ -11,6 +11,24 @@
 */
 
 
+// Replace memory_region_init_ram(readonly) by memory_region_init_rom()
+@@
+expression E1, E2, E3, E4, E5;
+symbol true;
+@@
+(
+- memory_region_init_ram(E1, E2, E3, E4, E5);
++ memory_region_init_rom(E1, E2, E3, E4, E5);
+  ... WHEN != E1
+- memory_region_set_readonly(E1, true);
+|
+- memory_region_init_ram_nomigrate(E1, E2, E3, E4, E5);
++ memory_region_init_rom_nomigrate(E1, E2, E3, E4, E5);
+  ... WHEN != E1
+- memory_region_set_readonly(E1, true);
+)
+
+
 // Replace by-hand memory_region_init_ram_nomigrate/vmstate_register_ram
 // code sequences with use of the new memory_region_init_ram function.
 // Similarly for the _rom and _rom_device functions.
Signature · 1 line
2.21.1
#6 lore raw

PATCH v2 5/32 hw/arm: Use memory_region_init_rom() with read-only regions

In reply to Philippe Mathieu-Daudé

This commit was produced with the Coccinelle script scripts/coccinelle/memory-region-housekeeping.cocci.

---
 hw/arm/exynos4210.c | 3 +--
 hw/arm/mainstone.c  | 3 +--
 hw/arm/omap_sx1.c   | 6 ++----
 hw/arm/palm.c       | 3 +--
 hw/arm/spitz.c      | 3 +--
 hw/arm/stellaris.c  | 3 +--
 hw/arm/tosa.c       | 3 +--
 7 files changed, 8 insertions(+), 16 deletions(-)
hw/arm/exynos4210.c +1 -2
index 59a27bdd68..3af6502a5e 100644
@@ -311,9 +311,8 @@ static void exynos4210_realize(DeviceState *socdev, Error **errp)
                                 &s->chipid_mem);
 
     /* Internal ROM */
-    memory_region_init_ram(&s->irom_mem, NULL, "exynos4210.irom",
+    memory_region_init_rom(&s->irom_mem, NULL, "exynos4210.irom",
                            EXYNOS4210_IROM_SIZE, &error_fatal);
-    memory_region_set_readonly(&s->irom_mem, true);
     memory_region_add_subregion(system_mem, EXYNOS4210_IROM_BASE_ADDR,
                                 &s->irom_mem);
     /* mirror of iROM */
hw/arm/mainstone.c +1 -2
index 6e64dfab50..05a806b422 100644
@@ -125,9 +125,8 @@ static void mainstone_common_init(MemoryRegion *address_space_mem,
     /* Setup CPU & memory */
     mpu = pxa270_init(address_space_mem, mainstone_binfo.ram_size,
                       machine->cpu_type);
-    memory_region_init_ram(rom, NULL, "mainstone.rom", MAINSTONE_ROM,
+    memory_region_init_rom(rom, NULL, "mainstone.rom", MAINSTONE_ROM,
                            &error_fatal);
-    memory_region_set_readonly(rom, true);
     memory_region_add_subregion(address_space_mem, 0, rom);
 
 #ifdef TARGET_WORDS_BIGENDIAN
hw/arm/omap_sx1.c +2 -4
index be245714db..6c3fd1b271 100644
@@ -126,9 +126,8 @@ static void sx1_init(MachineState *machine, const int version)
     mpu = omap310_mpu_init(dram, machine->cpu_type);
 
     /* External Flash (EMIFS) */
-    memory_region_init_ram(flash, NULL, "omap_sx1.flash0-0", flash_size,
+    memory_region_init_rom(flash, NULL, "omap_sx1.flash0-0", flash_size,
                            &error_fatal);
-    memory_region_set_readonly(flash, true);
     memory_region_add_subregion(address_space, OMAP_CS0_BASE, flash);
 
     memory_region_init_io(&cs[0], NULL, &static_ops, &cs0val,
@@ -168,9 +167,8 @@ static void sx1_init(MachineState *machine, const int version)
     if ((version == 1) &&
             (dinfo = drive_get(IF_PFLASH, 0, fl_idx)) != NULL) {
         MemoryRegion *flash_1 = g_new(MemoryRegion, 1);
-        memory_region_init_ram(flash_1, NULL, "omap_sx1.flash1-0",
+        memory_region_init_rom(flash_1, NULL, "omap_sx1.flash1-0",
                                flash1_size, &error_fatal);
-        memory_region_set_readonly(flash_1, true);
         memory_region_add_subregion(address_space, OMAP_CS1_BASE, flash_1);
 
         memory_region_init_io(&cs[1], NULL, &static_ops, &cs1val,
hw/arm/palm.c +1 -2
index 72eca8cc55..265d5891a6 100644
@@ -206,9 +206,8 @@ static void palmte_init(MachineState *machine)
     mpu = omap310_mpu_init(dram, machine->cpu_type);
 
     /* External Flash (EMIFS) */
-    memory_region_init_ram(flash, NULL, "palmte.flash", flash_size,
+    memory_region_init_rom(flash, NULL, "palmte.flash", flash_size,
                            &error_fatal);
-    memory_region_set_readonly(flash, true);
     memory_region_add_subregion(address_space_mem, OMAP_CS0_BASE, flash);
 
     memory_region_init_io(&cs[0], NULL, &static_ops, &cs0val, "palmte-cs0",
hw/arm/spitz.c +1 -2
index e001088103..1d27399721 100644
@@ -924,8 +924,7 @@ static void spitz_common_init(MachineState *machine,
 
     sl_flash_register(mpu, (model == spitz) ? FLASH_128M : FLASH_1024M);
 
-    memory_region_init_ram(rom, NULL, "spitz.rom", SPITZ_ROM, &error_fatal);
-    memory_region_set_readonly(rom, true);
+    memory_region_init_rom(rom, NULL, "spitz.rom", SPITZ_ROM, &error_fatal);
     memory_region_add_subregion(address_space_mem, 0, rom);
 
     /* Setup peripherals */
hw/arm/stellaris.c +1 -2
index 221a78674e..d136ba1a92 100644
@@ -1300,9 +1300,8 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
     sram_size = ((board->dc0 >> 18) + 1) * 1024;
 
     /* Flash programming is done via the SCU, so pretend it is ROM.  */
-    memory_region_init_ram(flash, NULL, "stellaris.flash", flash_size,
+    memory_region_init_rom(flash, NULL, "stellaris.flash", flash_size,
                            &error_fatal);
-    memory_region_set_readonly(flash, true);
     memory_region_add_subregion(system_memory, 0, flash);
 
     memory_region_init_ram(sram, NULL, "stellaris.sram", sram_size,
hw/arm/tosa.c +1 -2
index 4d95a1f3e2..5dee2d76c6 100644
@@ -226,8 +226,7 @@ static void tosa_init(MachineState *machine)
 
     mpu = pxa255_init(address_space_mem, tosa_binfo.ram_size);
 
-    memory_region_init_ram(rom, NULL, "tosa.rom", TOSA_ROM, &error_fatal);
-    memory_region_set_readonly(rom, true);
+    memory_region_init_rom(rom, NULL, "tosa.rom", TOSA_ROM, &error_fatal);
     memory_region_add_subregion(address_space_mem, 0, rom);
 
     tmio = tc6393xb_init(address_space_mem, 0x10000000,
Signature · 1 line
2.21.1
#10 lore raw

PATCH v2 6/32 hw/display: Use memory_region_init_rom() with read-only regions

In reply to Philippe Mathieu-Daudé

This commit was produced with the Coccinelle script scripts/coccinelle/memory-region-housekeeping.cocci.

---
 hw/display/cg3.c | 5 ++---
 hw/display/tcx.c | 5 ++---
 2 files changed, 4 insertions(+), 6 deletions(-)
hw/display/cg3.c +2 -3
index 4fb67c6b1c..a1ede10394 100644
@@ -287,9 +287,8 @@ static void cg3_initfn(Object *obj)
     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
     CG3State *s = CG3(obj);
 
-    memory_region_init_ram_nomigrate(&s->rom, obj, "cg3.prom", FCODE_MAX_ROM_SIZE,
-                           &error_fatal);
-    memory_region_set_readonly(&s->rom, true);
+    memory_region_init_rom_nomigrate(&s->rom, obj, "cg3.prom",
+                                     FCODE_MAX_ROM_SIZE, &error_fatal);
     sysbus_init_mmio(sbd, &s->rom);
 
     memory_region_init_io(&s->reg, obj, &cg3_reg_ops, s, "cg3.reg",
hw/display/tcx.c +2 -3
index ca458f94fe..76de16e8ea 100644
@@ -755,9 +755,8 @@ static void tcx_initfn(Object *obj)
     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
     TCXState *s = TCX(obj);
 
-    memory_region_init_ram_nomigrate(&s->rom, obj, "tcx.prom", FCODE_MAX_ROM_SIZE,
-                           &error_fatal);
-    memory_region_set_readonly(&s->rom, true);
+    memory_region_init_rom_nomigrate(&s->rom, obj, "tcx.prom",
+                                     FCODE_MAX_ROM_SIZE, &error_fatal);
     sysbus_init_mmio(sbd, &s->rom);
 
     /* 2/STIP : Stippler */
Signature · 1 line
2.21.1
#11 lore raw

PATCH v2 7/32 hw/mips: Use memory_region_init_rom() with read-only regions

In reply to Philippe Mathieu-Daudé

This commit was produced with the Coccinelle script scripts/coccinelle/memory-region-housekeeping.cocci.

---
 hw/mips/mips_fulong2e.c | 3 +--
 hw/mips/mips_jazz.c     | 6 ++----
 hw/mips/mips_mipssim.c  | 3 +--
 hw/mips/mips_r4k.c      | 3 +--
 4 files changed, 5 insertions(+), 10 deletions(-)
hw/mips/mips_fulong2e.c +1 -2
index 2e043cbb98..547a7d3a45 100644
@@ -319,9 +319,8 @@ static void mips_fulong2e_init(MachineState *machine)
 
     /* allocate RAM */
     memory_region_allocate_system_memory(ram, NULL, "fulong2e.ram", ram_size);
-    memory_region_init_ram(bios, NULL, "fulong2e.bios", BIOS_SIZE,
+    memory_region_init_rom(bios, NULL, "fulong2e.bios", BIOS_SIZE,
                            &error_fatal);
-    memory_region_set_readonly(bios, true);
 
     memory_region_add_subregion(address_space_mem, 0, ram);
     memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
hw/mips/mips_jazz.c +2 -4
index 66fd4d867d..cf47335ea5 100644
@@ -195,9 +195,8 @@ static void mips_jazz_init(MachineState *machine,
                                          machine->ram_size);
     memory_region_add_subregion(address_space, 0, ram);
 
-    memory_region_init_ram(bios, NULL, "mips_jazz.bios", MAGNUM_BIOS_SIZE,
+    memory_region_init_rom(bios, NULL, "mips_jazz.bios", MAGNUM_BIOS_SIZE,
                            &error_fatal);
-    memory_region_set_readonly(bios, true);
     memory_region_init_alias(bios2, NULL, "mips_jazz.bios", bios,
                              0, MAGNUM_BIOS_SIZE);
     memory_region_add_subregion(address_space, 0x1fc00000LL, bios);
@@ -263,9 +262,8 @@ static void mips_jazz_init(MachineState *machine,
         {
             /* Simple ROM, so user doesn't have to provide one */
             MemoryRegion *rom_mr = g_new(MemoryRegion, 1);
-            memory_region_init_ram(rom_mr, NULL, "g364fb.rom", 0x80000,
+            memory_region_init_rom(rom_mr, NULL, "g364fb.rom", 0x80000,
                                    &error_fatal);
-            memory_region_set_readonly(rom_mr, true);
             uint8_t *rom = memory_region_get_ram_ptr(rom_mr);
             memory_region_add_subregion(address_space, 0x60000000, rom_mr);
             rom[0] = 0x10; /* Mips G364 */
hw/mips/mips_mipssim.c +1 -2
index b934ca9d51..3fdbb21680 100644
@@ -169,9 +169,8 @@ mips_mipssim_init(MachineState *machine)
     /* Allocate RAM. */
     memory_region_allocate_system_memory(ram, NULL, "mips_mipssim.ram",
                                          ram_size);
-    memory_region_init_ram(bios, NULL, "mips_mipssim.bios", BIOS_SIZE,
+    memory_region_init_rom(bios, NULL, "mips_mipssim.bios", BIOS_SIZE,
                            &error_fatal);
-    memory_region_set_readonly(bios, true);
 
     memory_region_add_subregion(address_space_mem, 0, ram);
 
hw/mips/mips_r4k.c +1 -2
index b2aec434c3..54b286ceee 100644
@@ -241,9 +241,8 @@ void mips_r4k_init(MachineState *machine)
     dinfo = drive_get(IF_PFLASH, 0, 0);
     if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
         bios = g_new(MemoryRegion, 1);
-        memory_region_init_ram(bios, NULL, "mips_r4k.bios", BIOS_SIZE,
+        memory_region_init_rom(bios, NULL, "mips_r4k.bios", BIOS_SIZE,
                                &error_fatal);
-        memory_region_set_readonly(bios, true);
         memory_region_add_subregion(get_system_memory(), 0x1fc00000, bios);
 
         load_image_targphys(filename, 0x1fc00000, BIOS_SIZE);
Signature · 1 line
2.21.1
#12 lore raw

PATCH v2 8/32 hw/m68k: Use memory_region_init_rom() with read-only regions

In reply to Philippe Mathieu-Daudé

This commit was produced with the Coccinelle script scripts/coccinelle/memory-region-housekeeping.cocci.

---
 hw/m68k/q800.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/m68k/q800.c +1 -2
index 1e32363688..8083326d37 100644
@@ -402,13 +402,12 @@ static void q800_init(MachineState *machine)
         uint8_t *ptr;
         /* allocate and load BIOS */
         rom = g_malloc(sizeof(*rom));
-        memory_region_init_ram(rom, NULL, "m68k_mac.rom", MACROM_SIZE,
+        memory_region_init_rom(rom, NULL, "m68k_mac.rom", MACROM_SIZE,
                                &error_abort);
         if (bios_name == NULL) {
             bios_name = MACROM_FILENAME;
         }
         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
-        memory_region_set_readonly(rom, true);
         memory_region_add_subregion(get_system_memory(), MACROM_ADDR, rom);
 
         /* Load MacROM binary */
Signature · 1 line
2.21.1
Reviewed-by on v2 1/32
#2 lore raw

Re: [PATCH v2 01/32] memory: Correctly return alias region type

In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Since memory region aliases are neither rom nor ram, they are described as i/o, which is often incorrect. Return instead the type of the original region we are aliasing.

Alistair

Quoted 24 lines

---
Cc: [email protected]

 memory.c | 3 +++
 1 file changed, 3 insertions(+)
memory.c +3 -1
index aeaa8dcc9e..ce1179874e 100644
@@ -2818,6 +2818,9 @@ void address_space_destroy(AddressSpace *as)
 
 static const char *memory_region_type(MemoryRegion *mr)
 {
+    if (mr->alias) {
+        return memory_region_type(mr->alias);
+    }
     if (memory_region_is_ram_device(mr)) {
         return "ramd";
     } else if (memory_region_is_romd(mr)) {
--

2.21.1

Reviewed-by on v2 5/32
#7 lore raw

Re: [PATCH v2 05/32] hw/arm: Use memory_region_init_rom() with read-only regions

In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

This commit was produced with the Coccinelle script scripts/coccinelle/memory-region-housekeeping.cocci.

This looks good for the ROM regions, for the flash regions this doesn't change the current functionality but I'm not sure it's any clearer.

Either way though:

Alistair

Quoted 128 lines
---
 hw/arm/exynos4210.c | 3 +--
 hw/arm/mainstone.c  | 3 +--
 hw/arm/omap_sx1.c   | 6 ++----
 hw/arm/palm.c       | 3 +--
 hw/arm/spitz.c      | 3 +--
 hw/arm/stellaris.c  | 3 +--
 hw/arm/tosa.c       | 3 +--
 7 files changed, 8 insertions(+), 16 deletions(-)
hw/arm/exynos4210.c +1 -2
index 59a27bdd68..3af6502a5e 100644
@@ -311,9 +311,8 @@ static void exynos4210_realize(DeviceState *socdev, Error **errp)
                                 &s->chipid_mem);
 
     /* Internal ROM */
-    memory_region_init_ram(&s->irom_mem, NULL, "exynos4210.irom",
+    memory_region_init_rom(&s->irom_mem, NULL, "exynos4210.irom",
                            EXYNOS4210_IROM_SIZE, &error_fatal);
-    memory_region_set_readonly(&s->irom_mem, true);
     memory_region_add_subregion(system_mem, EXYNOS4210_IROM_BASE_ADDR,
                                 &s->irom_mem);
     /* mirror of iROM */
hw/arm/mainstone.c +1 -2
index 6e64dfab50..05a806b422 100644
@@ -125,9 +125,8 @@ static void mainstone_common_init(MemoryRegion *address_space_mem,
     /* Setup CPU & memory */
     mpu = pxa270_init(address_space_mem, mainstone_binfo.ram_size,
                       machine->cpu_type);
-    memory_region_init_ram(rom, NULL, "mainstone.rom", MAINSTONE_ROM,
+    memory_region_init_rom(rom, NULL, "mainstone.rom", MAINSTONE_ROM,
                            &error_fatal);
-    memory_region_set_readonly(rom, true);
     memory_region_add_subregion(address_space_mem, 0, rom);
 
 #ifdef TARGET_WORDS_BIGENDIAN
hw/arm/omap_sx1.c +2 -4
index be245714db..6c3fd1b271 100644
@@ -126,9 +126,8 @@ static void sx1_init(MachineState *machine, const int version)
     mpu = omap310_mpu_init(dram, machine->cpu_type);
 
     /* External Flash (EMIFS) */
-    memory_region_init_ram(flash, NULL, "omap_sx1.flash0-0", flash_size,
+    memory_region_init_rom(flash, NULL, "omap_sx1.flash0-0", flash_size,
                            &error_fatal);
-    memory_region_set_readonly(flash, true);
     memory_region_add_subregion(address_space, OMAP_CS0_BASE, flash);
 
     memory_region_init_io(&cs[0], NULL, &static_ops, &cs0val,
@@ -168,9 +167,8 @@ static void sx1_init(MachineState *machine, const int version)
     if ((version == 1) &&
             (dinfo = drive_get(IF_PFLASH, 0, fl_idx)) != NULL) {
         MemoryRegion *flash_1 = g_new(MemoryRegion, 1);
-        memory_region_init_ram(flash_1, NULL, "omap_sx1.flash1-0",
+        memory_region_init_rom(flash_1, NULL, "omap_sx1.flash1-0",
                                flash1_size, &error_fatal);
-        memory_region_set_readonly(flash_1, true);
         memory_region_add_subregion(address_space, OMAP_CS1_BASE, flash_1);
 
         memory_region_init_io(&cs[1], NULL, &static_ops, &cs1val,
hw/arm/palm.c +1 -2
index 72eca8cc55..265d5891a6 100644
@@ -206,9 +206,8 @@ static void palmte_init(MachineState *machine)
     mpu = omap310_mpu_init(dram, machine->cpu_type);
 
     /* External Flash (EMIFS) */
-    memory_region_init_ram(flash, NULL, "palmte.flash", flash_size,
+    memory_region_init_rom(flash, NULL, "palmte.flash", flash_size,
                            &error_fatal);
-    memory_region_set_readonly(flash, true);
     memory_region_add_subregion(address_space_mem, OMAP_CS0_BASE, flash);
 
     memory_region_init_io(&cs[0], NULL, &static_ops, &cs0val, "palmte-cs0",
hw/arm/spitz.c +1 -2
index e001088103..1d27399721 100644
@@ -924,8 +924,7 @@ static void spitz_common_init(MachineState *machine,
 
     sl_flash_register(mpu, (model == spitz) ? FLASH_128M : FLASH_1024M);
 
-    memory_region_init_ram(rom, NULL, "spitz.rom", SPITZ_ROM, &error_fatal);
-    memory_region_set_readonly(rom, true);
+    memory_region_init_rom(rom, NULL, "spitz.rom", SPITZ_ROM, &error_fatal);
     memory_region_add_subregion(address_space_mem, 0, rom);
 
     /* Setup peripherals */
hw/arm/stellaris.c +1 -2
index 221a78674e..d136ba1a92 100644
@@ -1300,9 +1300,8 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
     sram_size = ((board->dc0 >> 18) + 1) * 1024;
 
     /* Flash programming is done via the SCU, so pretend it is ROM.  */
-    memory_region_init_ram(flash, NULL, "stellaris.flash", flash_size,
+    memory_region_init_rom(flash, NULL, "stellaris.flash", flash_size,
                            &error_fatal);
-    memory_region_set_readonly(flash, true);
     memory_region_add_subregion(system_memory, 0, flash);
 
     memory_region_init_ram(sram, NULL, "stellaris.sram", sram_size,
hw/arm/tosa.c +1 -3
index 4d95a1f3e2..5dee2d76c6 100644
@@ -226,8 +226,7 @@ static void tosa_init(MachineState *machine)
 
     mpu = pxa255_init(address_space_mem, tosa_binfo.ram_size);
 
-    memory_region_init_ram(rom, NULL, "tosa.rom", TOSA_ROM, &error_fatal);
-    memory_region_set_readonly(rom, true);
+    memory_region_init_rom(rom, NULL, "tosa.rom", TOSA_ROM, &error_fatal);
     memory_region_add_subregion(address_space_mem, 0, rom);
 
     tmio = tc6393xb_init(address_space_mem, 0x10000000,
--

2.21.1

#8 lore raw

Re: [PATCH v2 05/32] hw/arm: Use memory_region_init_rom() with read-only regions

Quoted 11 lines · Alistair Francis
Quoted 5 lines · Philippe Mathieu-Daudé

This commit was produced with the Coccinelle script scripts/coccinelle/memory-region-housekeeping.cocci.

This looks good for the ROM regions, for the flash regions this doesn't change the current functionality but I'm not sure it's any clearer.

Less code makes easier review.

These regions behave as ROM on the bus, any write to the address space is illegal (they are programmable via I/O registers in another address space).

Quoted 4 lines

Either way though:

Thanks, do you want me to improve the commit description?

Quoted 132 lines

Alistair

Quoted 128 lines
---
  hw/arm/exynos4210.c | 3 +--
  hw/arm/mainstone.c  | 3 +--
  hw/arm/omap_sx1.c   | 6 ++----
  hw/arm/palm.c       | 3 +--
  hw/arm/spitz.c      | 3 +--
  hw/arm/stellaris.c  | 3 +--
  hw/arm/tosa.c       | 3 +--
  7 files changed, 8 insertions(+), 16 deletions(-)
hw/arm/exynos4210.c +1 -2
index 59a27bdd68..3af6502a5e 100644
@@ -311,9 +311,8 @@ static void exynos4210_realize(DeviceState *socdev, Error **errp)
                                  &s->chipid_mem);
 
      /* Internal ROM */
-    memory_region_init_ram(&s->irom_mem, NULL, "exynos4210.irom",
+    memory_region_init_rom(&s->irom_mem, NULL, "exynos4210.irom",
                             EXYNOS4210_IROM_SIZE, &error_fatal);
-    memory_region_set_readonly(&s->irom_mem, true);
      memory_region_add_subregion(system_mem, EXYNOS4210_IROM_BASE_ADDR,
                                  &s->irom_mem);
      /* mirror of iROM */
hw/arm/mainstone.c +1 -2
index 6e64dfab50..05a806b422 100644
@@ -125,9 +125,8 @@ static void mainstone_common_init(MemoryRegion *address_space_mem,
      /* Setup CPU & memory */
      mpu = pxa270_init(address_space_mem, mainstone_binfo.ram_size,
                        machine->cpu_type);
-    memory_region_init_ram(rom, NULL, "mainstone.rom", MAINSTONE_ROM,
+    memory_region_init_rom(rom, NULL, "mainstone.rom", MAINSTONE_ROM,
                             &error_fatal);
-    memory_region_set_readonly(rom, true);
      memory_region_add_subregion(address_space_mem, 0, rom);
 
  #ifdef TARGET_WORDS_BIGENDIAN
hw/arm/omap_sx1.c +2 -4
index be245714db..6c3fd1b271 100644
@@ -126,9 +126,8 @@ static void sx1_init(MachineState *machine, const int version)
      mpu = omap310_mpu_init(dram, machine->cpu_type);
 
      /* External Flash (EMIFS) */
-    memory_region_init_ram(flash, NULL, "omap_sx1.flash0-0", flash_size,
+    memory_region_init_rom(flash, NULL, "omap_sx1.flash0-0", flash_size,
                             &error_fatal);
-    memory_region_set_readonly(flash, true);
      memory_region_add_subregion(address_space, OMAP_CS0_BASE, flash);
 
      memory_region_init_io(&cs[0], NULL, &static_ops, &cs0val,
@@ -168,9 +167,8 @@ static void sx1_init(MachineState *machine, const int version)
      if ((version == 1) &&
              (dinfo = drive_get(IF_PFLASH, 0, fl_idx)) != NULL) {
          MemoryRegion *flash_1 = g_new(MemoryRegion, 1);
-        memory_region_init_ram(flash_1, NULL, "omap_sx1.flash1-0",
+        memory_region_init_rom(flash_1, NULL, "omap_sx1.flash1-0",
                                 flash1_size, &error_fatal);
-        memory_region_set_readonly(flash_1, true);
          memory_region_add_subregion(address_space, OMAP_CS1_BASE, flash_1);
 
          memory_region_init_io(&cs[1], NULL, &static_ops, &cs1val,
hw/arm/palm.c +1 -2
index 72eca8cc55..265d5891a6 100644
@@ -206,9 +206,8 @@ static void palmte_init(MachineState *machine)
      mpu = omap310_mpu_init(dram, machine->cpu_type);
 
      /* External Flash (EMIFS) */
-    memory_region_init_ram(flash, NULL, "palmte.flash", flash_size,
+    memory_region_init_rom(flash, NULL, "palmte.flash", flash_size,
                             &error_fatal);
-    memory_region_set_readonly(flash, true);
      memory_region_add_subregion(address_space_mem, OMAP_CS0_BASE, flash);
 
      memory_region_init_io(&cs[0], NULL, &static_ops, &cs0val, "palmte-cs0",
hw/arm/spitz.c +1 -2
index e001088103..1d27399721 100644
@@ -924,8 +924,7 @@ static void spitz_common_init(MachineState *machine,
 
      sl_flash_register(mpu, (model == spitz) ? FLASH_128M : FLASH_1024M);
 
-    memory_region_init_ram(rom, NULL, "spitz.rom", SPITZ_ROM, &error_fatal);
-    memory_region_set_readonly(rom, true);
+    memory_region_init_rom(rom, NULL, "spitz.rom", SPITZ_ROM, &error_fatal);
      memory_region_add_subregion(address_space_mem, 0, rom);
 
      /* Setup peripherals */
hw/arm/stellaris.c +1 -2
index 221a78674e..d136ba1a92 100644
@@ -1300,9 +1300,8 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
      sram_size = ((board->dc0 >> 18) + 1) * 1024;
 
      /* Flash programming is done via the SCU, so pretend it is ROM.  */
-    memory_region_init_ram(flash, NULL, "stellaris.flash", flash_size,
+    memory_region_init_rom(flash, NULL, "stellaris.flash", flash_size,
                             &error_fatal);
-    memory_region_set_readonly(flash, true);
      memory_region_add_subregion(system_memory, 0, flash);
 
      memory_region_init_ram(sram, NULL, "stellaris.sram", sram_size,
hw/arm/tosa.c +1 -3
index 4d95a1f3e2..5dee2d76c6 100644
@@ -226,8 +226,7 @@ static void tosa_init(MachineState *machine)
 
      mpu = pxa255_init(address_space_mem, tosa_binfo.ram_size);
 
-    memory_region_init_ram(rom, NULL, "tosa.rom", TOSA_ROM, &error_fatal);
-    memory_region_set_readonly(rom, true);
+    memory_region_init_rom(rom, NULL, "tosa.rom", TOSA_ROM, &error_fatal);
      memory_region_add_subregion(address_space_mem, 0, rom);
 
      tmio = tc6393xb_init(address_space_mem, 0x10000000,
--

2.21.1

#9 lore raw

Re: [PATCH v2 05/32] hw/arm: Use memory_region_init_rom() with read-only regions

Quoted 14 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Alistair Francis
Quoted 5 lines · Philippe Mathieu-Daudé

This commit was produced with the Coccinelle script scripts/coccinelle/memory-region-housekeeping.cocci.

This looks good for the ROM regions, for the flash regions this doesn't change the current functionality but I'm not sure it's any clearer.

Quoted 5 lines

Less code makes easier review.

These regions behave as ROM on the bus, any write to the address space is illegal (they are programmable via I/O registers in another address space).

Good point.

Quoted 7 lines
Quoted 5 lines

Either way though:

Thanks, do you want me to improve the commit description?

Nope, it's fine :)

Alistair

Quoted 134 lines
Quoted 133 lines

Alistair

Quoted 128 lines
---
  hw/arm/exynos4210.c | 3 +--
  hw/arm/mainstone.c  | 3 +--
  hw/arm/omap_sx1.c   | 6 ++----
  hw/arm/palm.c       | 3 +--
  hw/arm/spitz.c      | 3 +--
  hw/arm/stellaris.c  | 3 +--
  hw/arm/tosa.c       | 3 +--
  7 files changed, 8 insertions(+), 16 deletions(-)
hw/arm/exynos4210.c +1 -2
index 59a27bdd68..3af6502a5e 100644
@@ -311,9 +311,8 @@ static void exynos4210_realize(DeviceState *socdev, Error **errp)
                                  &s->chipid_mem);
 
      /* Internal ROM */
-    memory_region_init_ram(&s->irom_mem, NULL, "exynos4210.irom",
+    memory_region_init_rom(&s->irom_mem, NULL, "exynos4210.irom",
                             EXYNOS4210_IROM_SIZE, &error_fatal);
-    memory_region_set_readonly(&s->irom_mem, true);
      memory_region_add_subregion(system_mem, EXYNOS4210_IROM_BASE_ADDR,
                                  &s->irom_mem);
      /* mirror of iROM */
hw/arm/mainstone.c +1 -2
index 6e64dfab50..05a806b422 100644
@@ -125,9 +125,8 @@ static void mainstone_common_init(MemoryRegion *address_space_mem,
      /* Setup CPU & memory */
      mpu = pxa270_init(address_space_mem, mainstone_binfo.ram_size,
                        machine->cpu_type);
-    memory_region_init_ram(rom, NULL, "mainstone.rom", MAINSTONE_ROM,
+    memory_region_init_rom(rom, NULL, "mainstone.rom", MAINSTONE_ROM,
                             &error_fatal);
-    memory_region_set_readonly(rom, true);
      memory_region_add_subregion(address_space_mem, 0, rom);
 
  #ifdef TARGET_WORDS_BIGENDIAN
hw/arm/omap_sx1.c +2 -4
index be245714db..6c3fd1b271 100644
@@ -126,9 +126,8 @@ static void sx1_init(MachineState *machine, const int version)
      mpu = omap310_mpu_init(dram, machine->cpu_type);
 
      /* External Flash (EMIFS) */
-    memory_region_init_ram(flash, NULL, "omap_sx1.flash0-0", flash_size,
+    memory_region_init_rom(flash, NULL, "omap_sx1.flash0-0", flash_size,
                             &error_fatal);
-    memory_region_set_readonly(flash, true);
      memory_region_add_subregion(address_space, OMAP_CS0_BASE, flash);
 
      memory_region_init_io(&cs[0], NULL, &static_ops, &cs0val,
@@ -168,9 +167,8 @@ static void sx1_init(MachineState *machine, const int version)
      if ((version == 1) &&
              (dinfo = drive_get(IF_PFLASH, 0, fl_idx)) != NULL) {
          MemoryRegion *flash_1 = g_new(MemoryRegion, 1);
-        memory_region_init_ram(flash_1, NULL, "omap_sx1.flash1-0",
+        memory_region_init_rom(flash_1, NULL, "omap_sx1.flash1-0",
                                 flash1_size, &error_fatal);
-        memory_region_set_readonly(flash_1, true);
          memory_region_add_subregion(address_space, OMAP_CS1_BASE, flash_1);
 
          memory_region_init_io(&cs[1], NULL, &static_ops, &cs1val,
hw/arm/palm.c +1 -2
index 72eca8cc55..265d5891a6 100644
@@ -206,9 +206,8 @@ static void palmte_init(MachineState *machine)
      mpu = omap310_mpu_init(dram, machine->cpu_type);
 
      /* External Flash (EMIFS) */
-    memory_region_init_ram(flash, NULL, "palmte.flash", flash_size,
+    memory_region_init_rom(flash, NULL, "palmte.flash", flash_size,
                             &error_fatal);
-    memory_region_set_readonly(flash, true);
      memory_region_add_subregion(address_space_mem, OMAP_CS0_BASE, flash);
 
      memory_region_init_io(&cs[0], NULL, &static_ops, &cs0val, "palmte-cs0",
hw/arm/spitz.c +1 -2
index e001088103..1d27399721 100644
@@ -924,8 +924,7 @@ static void spitz_common_init(MachineState *machine,
 
      sl_flash_register(mpu, (model == spitz) ? FLASH_128M : FLASH_1024M);
 
-    memory_region_init_ram(rom, NULL, "spitz.rom", SPITZ_ROM, &error_fatal);
-    memory_region_set_readonly(rom, true);
+    memory_region_init_rom(rom, NULL, "spitz.rom", SPITZ_ROM, &error_fatal);
      memory_region_add_subregion(address_space_mem, 0, rom);
 
      /* Setup peripherals */
hw/arm/stellaris.c +1 -2
index 221a78674e..d136ba1a92 100644
@@ -1300,9 +1300,8 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
      sram_size = ((board->dc0 >> 18) + 1) * 1024;
 
      /* Flash programming is done via the SCU, so pretend it is ROM.  */
-    memory_region_init_ram(flash, NULL, "stellaris.flash", flash_size,
+    memory_region_init_rom(flash, NULL, "stellaris.flash", flash_size,
                             &error_fatal);
-    memory_region_set_readonly(flash, true);
      memory_region_add_subregion(system_memory, 0, flash);
 
      memory_region_init_ram(sram, NULL, "stellaris.sram", sram_size,
hw/arm/tosa.c +1 -3
index 4d95a1f3e2..5dee2d76c6 100644
@@ -226,8 +226,7 @@ static void tosa_init(MachineState *machine)
 
      mpu = pxa255_init(address_space_mem, tosa_binfo.ram_size);
 
-    memory_region_init_ram(rom, NULL, "tosa.rom", TOSA_ROM, &error_fatal);
-    memory_region_set_readonly(rom, true);
+    memory_region_init_rom(rom, NULL, "tosa.rom", TOSA_ROM, &error_fatal);
      memory_region_add_subregion(address_space_mem, 0, rom);
 
      tmio = tc6393xb_init(address_space_mem, 0x10000000,
--

2.21.1