qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: qemu-devel@nongnu.org
Subject: [PULL 02/45] hw/virtio/virtio-mem: Convert VIRTIO_MEM_HAS_LEGACY_GUESTS to runtime
Date: Tue, 21 Oct 2025 22:46:16 +0200	[thread overview]
Message-ID: <20251021204700.56072-3-philmd@linaro.org> (raw)
In-Reply-To: <20251021204700.56072-1-philmd@linaro.org>

Check legacy guests support at runtime: instead of evaluating
the VIRTIO_MEM_HAS_LEGACY_GUESTS definition at compile time,
call target_arch() to detect which target is being run at runtime.
Register virtio_mem_legacy_guests_properties[] at runtime.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Acked-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Message-Id: <20250502214551.80401-5-philmd@linaro.org>
---
 hw/virtio/virtio-mem.c | 76 ++++++++++++++++++++++++------------------
 1 file changed, 43 insertions(+), 33 deletions(-)

diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c
index f16445b9347..ae7c13e33cf 100644
--- a/hw/virtio/virtio-mem.c
+++ b/hw/virtio/virtio-mem.c
@@ -34,13 +34,21 @@
 
 static const VMStateDescription vmstate_virtio_mem_device_early;
 
-/*
- * We only had legacy x86 guests that did not support
- * VIRTIO_MEM_F_UNPLUGGED_INACCESSIBLE. Other targets don't have legacy guests.
- */
-#if defined(TARGET_X86_64) || defined(TARGET_I386)
-#define VIRTIO_MEM_HAS_LEGACY_GUESTS
-#endif
+static bool virtio_mem_has_legacy_guests(void)
+{
+    /*
+     * We only had legacy x86 guests that did not support
+     * VIRTIO_MEM_F_UNPLUGGED_INACCESSIBLE. Other targets don't have
+     * legacy guests.
+     */
+    switch (target_arch()) {
+    case SYS_EMU_TARGET_I386:
+    case SYS_EMU_TARGET_X86_64:
+        return true;
+    default:
+        return false;
+    }
+}
 
 /*
  * Let's not allow blocks smaller than 1 MiB, for example, to keep the tracking
@@ -144,7 +152,6 @@ static uint64_t virtio_mem_default_block_size(RAMBlock *rb)
     return MAX(page_size, VIRTIO_MEM_MIN_BLOCK_SIZE);
 }
 
-#if defined(VIRTIO_MEM_HAS_LEGACY_GUESTS)
 static bool virtio_mem_has_shared_zeropage(RAMBlock *rb)
 {
     /*
@@ -155,7 +162,6 @@ static bool virtio_mem_has_shared_zeropage(RAMBlock *rb)
     return !qemu_ram_is_shared(rb) && qemu_ram_get_fd(rb) < 0 &&
            qemu_ram_pagesize(rb) == qemu_real_host_page_size();
 }
-#endif /* VIRTIO_MEM_HAS_LEGACY_GUESTS */
 
 /*
  * Size the usable region bigger than the requested size if possible. Esp.
@@ -983,28 +989,28 @@ static void virtio_mem_device_realize(DeviceState *dev, Error **errp)
     rb = vmem->memdev->mr.ram_block;
     page_size = qemu_ram_pagesize(rb);
 
-#if defined(VIRTIO_MEM_HAS_LEGACY_GUESTS)
-    switch (vmem->unplugged_inaccessible) {
-    case ON_OFF_AUTO_AUTO:
-        if (virtio_mem_has_shared_zeropage(rb)) {
-            vmem->unplugged_inaccessible = ON_OFF_AUTO_OFF;
-        } else {
-            vmem->unplugged_inaccessible = ON_OFF_AUTO_ON;
+    if (virtio_mem_has_legacy_guests()) {
+        switch (vmem->unplugged_inaccessible) {
+        case ON_OFF_AUTO_AUTO:
+            if (virtio_mem_has_shared_zeropage(rb)) {
+                vmem->unplugged_inaccessible = ON_OFF_AUTO_OFF;
+            } else {
+                vmem->unplugged_inaccessible = ON_OFF_AUTO_ON;
+            }
+            break;
+        case ON_OFF_AUTO_OFF:
+            if (!virtio_mem_has_shared_zeropage(rb)) {
+                warn_report("'%s' property set to 'off' with a memdev that does"
+                            " not support the shared zeropage.",
+                            VIRTIO_MEM_UNPLUGGED_INACCESSIBLE_PROP);
+            }
+            break;
+        default:
+            break;
         }
-        break;
-    case ON_OFF_AUTO_OFF:
-        if (!virtio_mem_has_shared_zeropage(rb)) {
-            warn_report("'%s' property set to 'off' with a memdev that does"
-                        " not support the shared zeropage.",
-                        VIRTIO_MEM_UNPLUGGED_INACCESSIBLE_PROP);
-        }
-        break;
-    default:
-        break;
+    } else {
+        vmem->unplugged_inaccessible = ON_OFF_AUTO_ON;
     }
-#else /* VIRTIO_MEM_HAS_LEGACY_GUESTS */
-    vmem->unplugged_inaccessible = ON_OFF_AUTO_ON;
-#endif /* VIRTIO_MEM_HAS_LEGACY_GUESTS */
 
     if (vmem->dynamic_memslots &&
         vmem->unplugged_inaccessible != ON_OFF_AUTO_ON) {
@@ -1701,16 +1707,17 @@ static const Property virtio_mem_properties[] = {
     DEFINE_PROP_BOOL(VIRTIO_MEM_PREALLOC_PROP, VirtIOMEM, prealloc, false),
     DEFINE_PROP_LINK(VIRTIO_MEM_MEMDEV_PROP, VirtIOMEM, memdev,
                      TYPE_MEMORY_BACKEND, HostMemoryBackend *),
-#if defined(VIRTIO_MEM_HAS_LEGACY_GUESTS)
-    DEFINE_PROP_ON_OFF_AUTO(VIRTIO_MEM_UNPLUGGED_INACCESSIBLE_PROP, VirtIOMEM,
-                            unplugged_inaccessible, ON_OFF_AUTO_ON),
-#endif
     DEFINE_PROP_BOOL(VIRTIO_MEM_EARLY_MIGRATION_PROP, VirtIOMEM,
                      early_migration, true),
     DEFINE_PROP_BOOL(VIRTIO_MEM_DYNAMIC_MEMSLOTS_PROP, VirtIOMEM,
                      dynamic_memslots, false),
 };
 
+static const Property virtio_mem_legacy_guests_properties[] = {
+    DEFINE_PROP_ON_OFF_AUTO(VIRTIO_MEM_UNPLUGGED_INACCESSIBLE_PROP, VirtIOMEM,
+                            unplugged_inaccessible, ON_OFF_AUTO_ON),
+};
+
 static uint64_t virtio_mem_rdm_get_min_granularity(const RamDiscardManager *rdm,
                                                    const MemoryRegion *mr)
 {
@@ -1862,6 +1869,9 @@ static void virtio_mem_class_init(ObjectClass *klass, const void *data)
     RamDiscardManagerClass *rdmc = RAM_DISCARD_MANAGER_CLASS(klass);
 
     device_class_set_props(dc, virtio_mem_properties);
+    if (virtio_mem_has_legacy_guests()) {
+        device_class_set_props(dc, virtio_mem_legacy_guests_properties);
+    }
     dc->vmsd = &vmstate_virtio_mem;
 
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
-- 
2.51.0



  parent reply	other threads:[~2025-10-21 20:47 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-21 20:46 [PULL 00/45] Misc HW patches for 2025-10-21 Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 01/45] hw/virtio/virtio-mem: Convert VIRTIO_MEM_USABLE_EXTENT to runtime Philippe Mathieu-Daudé
2025-10-21 20:46 ` Philippe Mathieu-Daudé [this message]
2025-10-21 20:46 ` [PULL 03/45] hw/virtio: Compile virtio-mem.c once Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 04/45] hw/pci-host/raven: Simplify direct config access address decoding Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 05/45] hw/pci-host/raven: Rename direct config access ops Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 06/45] hw/pci-host/raven: Use correct parameter in direct " Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 07/45] hw/core: Filter machine list available for a particular target binary Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 08/45] hw/core/machine: Allow dynamic registration of valid CPU types Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 09/45] hw/core: Introduce MachineClass::get_default_cpu_type() helper Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 10/45] hw/boards: Move DEFINE_MACHINE() definition closer to its doc string Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 11/45] hw/boards: Extend DEFINE_MACHINE macro to cover more use cases Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 12/45] hw/boards: Introduce DEFINE_MACHINE_WITH_INTERFACE_ARRAY() macro Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 13/45] hw/i2c/smbus_eeprom: Add minimum write recovery time for DDR2 Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 14/45] hw/ppc/e500: Check for compatible CPU type instead of aborting ungracefully Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 15/45] hw/openrisc/openrisc_sim: Avoid buffer overflow build error Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 16/45] hw/xen: pass PCI domain to xc_physdev_map_pirq_msi() Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 17/45] hw/core/register: remove the REGISTER device type Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 18/45] hw/core/register: add the REGISTER_ARRAY type Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 19/45] hw/core/register: remove the calls to `register_finalize_block' Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 20/45] hw/core/register: remove the `register_finalize_block' function Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 21/45] hw/net/can/xlnx-versal-canfd: refactor the banked registers logic Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 22/45] hw/net/can/xlnx-versal-canfd: remove register API usage for banked regs Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 23/45] hw/ppc/prep: Always create prep-systemio Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 24/45] hw/timer/i8254: Add I/O trace events Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 25/45] hw/audio/pcspk: " Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 26/45] hw/rtc/mc146818rtc: Convert CMOS_DPRINTF() into " Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 27/45] hw/rtc/mc146818rtc: Use ARRAY_SIZE macro Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 28/45] hw/rtc/mc146818rtc: Assert correct usage of mc146818rtc_set_cmos_data() Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 29/45] hw/ide/ide-internal: Move dma_buf_commit() into ide "namespace" Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 30/45] hw/i386/apic: Prefer APICCommonState over DeviceState Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 31/45] hw/i386/apic: Ensure own APIC use in apic_msr_{read, write} Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 32/45] hw/intc/apic: Pass APICCommonState to apic_register_{read, write} Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 33/45] tests/qtest/ds1338-test: Reuse from_bcd() Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 34/45] hw/audio: improve error reports Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 35/45] hw/audio: rename model list function Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 36/45] hw/audio: remove global pcspk Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 37/45] hw/pcspk: use explicitly the required PIT types Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 38/45] hw/pcspk: make 'pit' a class property Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 39/45] hw/pcspk: check the "pit" is set Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 40/45] hw/audio: replace AUD_log() usage Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 41/45] hw/ppc/spapr: Rename resize_hpt_err to errp Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 42/45] qemu/target-info: Include missing 'qapi-types-common.h' header Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 43/45] MAINTAINERS: Add missing machine name in the Alpha section Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 44/45] docs: update -soundhw -> -device list Philippe Mathieu-Daudé
2025-10-21 20:46 ` [PULL 45/45] docs: Update mentions of removed '-soundhw' command line option Philippe Mathieu-Daudé
2025-10-22 14:29 ` [PULL 00/45] Misc HW patches for 2025-10-21 Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251021204700.56072-3-philmd@linaro.org \
    --to=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).