Commit 23f93333cd for qemu.org

commit 23f93333cd0a9c82f9d1e0d920a9b194426fd8d8
Author: Brian Cain <brian.cain@oss.qualcomm.com>
Date:   Tue Nov 24 11:03:14 2026 -0800

    hw/hexagon: honor configured RAM backends

    The Hexagon machines consumed the MachineState memory-backend
    configuration but always created and mapped a separate DDR region, so
    a memory backend given with -machine memory-backend= was never used as
    guest memory.  Map the machine's configured RAM when present, so that
    file-backed and shared memory (as used by the migration tests) is
    actually guest memory.  Keep the board-allocated RAM as the fallback.

    Add a regression test that writes guest DDR and verifies the bytes
    through the backing file.  It fails without this change.

    Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
    Signed-off-by: Brian Cain <brian.cain@oss.qualcomm.com>

diff --git a/hw/hexagon/hex-subsys.c b/hw/hexagon/hex-subsys.c
index 52ec0cadac..1103d2b556 100644
--- a/hw/hexagon/hex-subsys.c
+++ b/hw/hexagon/hex-subsys.c
@@ -163,12 +163,16 @@ void hex_subsys_create(HexagonCommonMachineState *hms,
 {
     MachineState *machine = MACHINE(hms);
     MemoryRegion *sysmem = get_system_memory();
+    MemoryRegion *ram = machine->ram;
     size_t i;

     /* Main DDR. */
-    memory_region_init_ram(&hms->ram, NULL, "ddr.ram", machine->ram_size,
-                           &error_fatal);
-    memory_region_add_subregion(sysmem, m_cfg->ddr_base, &hms->ram);
+    if (!ram) {
+        memory_region_init_ram(&hms->ram, NULL, "ddr.ram", machine->ram_size,
+                               &error_fatal);
+        ram = &hms->ram;
+    }
+    memory_region_add_subregion(sysmem, m_cfg->ddr_base, ram);

     /* Config-table ROM and the blob that backs it. */
     memory_region_init_rom(&hms->cfgtable_rom, NULL, "config_table.rom",
diff --git a/tests/qtest/hexagon-config-test.c b/tests/qtest/hexagon-config-test.c
index 9554fce57c..d4dc9a2d3f 100644
--- a/tests/qtest/hexagon-config-test.c
+++ b/tests/qtest/hexagon-config-test.c
@@ -60,6 +60,31 @@ static void test_config_table(gconstpointer data)
     qtest_end();
 }

+/* The virt machine maps DDR at guest physical address 0. */
+static void test_memory_backend(void)
+{
+    g_autofree char *path = NULL;
+    g_autofree char *args = NULL;
+    const uint8_t expected[] = { 0x12, 0x34, 0x56, 0x78 };
+    uint8_t actual[4];
+    int fd = g_file_open_tmp("hexagon-ram-XXXXXX", &path, NULL);
+
+    g_assert_cmpint(fd, >=, 0);
+    g_assert_cmpint(ftruncate(fd, 16 * 1024 * 1024), ==, 0);
+    args = g_strdup_printf("-machine virt,memory-backend=ram -m 16M "
+                           "-object memory-backend-file,id=ram,size=16M,"
+                           "share=on,mem-path=%s", path);
+    qtest_start(args);
+    /* QEMU has the file open now, so nothing is left behind on failure. */
+    unlink(path);
+    writel(0x1000, 0x78563412);
+    g_assert_cmpint(lseek(fd, 0x1000, SEEK_SET), ==, 0x1000);
+    g_assert_cmpint(read(fd, actual, sizeof(actual)), ==, sizeof(actual));
+    g_assert_cmpmem(actual, sizeof(actual), expected, sizeof(expected));
+    qtest_end();
+    close(fd);
+}
+
 int main(int argc, char **argv)
 {
     size_t i;
@@ -74,5 +99,6 @@ int main(int argc, char **argv)
         qtest_add_data_func(path, mc, test_config_table);
     }

+    qtest_add_func("/config/memory-backend", test_memory_backend);
     return g_test_run();
 }