From 0fb124dbfa135945fb892d0d2b7a427cdc59220d Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Thu, 25 Mar 2021 15:33:08 +0000 Subject: machine: Provide a function to check the dynamic sysbus allowlist Provide a new function dynamic_sysbus_dev_allowed() which checks the per-machine list of permitted dynamic sysbus devices and returns a boolean result indicating whether the device is allowed. We can use this in the implementation of validate_sysbus_device(), but we will also need it so that machine hotplug callbacks can validate devices rather than assuming that any sysbus device might be hotpluggable into the platform bus. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Reviewed-by: Mark Cave-Ayland Reviewed-by: Eric Auger Message-id: 20210325153310.9131-3-peter.maydell@linaro.org --- hw/core/machine.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'hw') diff --git a/hw/core/machine.c b/hw/core/machine.c index 9935c6d..8d97094 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -529,20 +529,31 @@ void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type) QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type)); } -static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque) +bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev) { - MachineState *machine = opaque; - MachineClass *mc = MACHINE_GET_CLASS(machine); bool allowed = false; strList *wl; + Object *obj = OBJECT(dev); + + if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) { + return false; + } for (wl = mc->allowed_dynamic_sysbus_devices; !allowed && wl; wl = wl->next) { - allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value); + allowed |= !!object_dynamic_cast(obj, wl->value); } - if (!allowed) { + return allowed; +} + +static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque) +{ + MachineState *machine = opaque; + MachineClass *mc = MACHINE_GET_CLASS(machine); + + if (!device_is_dynamic_sysbus(mc, DEVICE(sbdev))) { error_report("Option '-device %s' cannot be handled by this machine", object_class_get_name(object_get_class(OBJECT(sbdev)))); exit(1); -- cgit v1.1 From 37fce4dde15f7f7891be89972629d96f851f9815 Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Thu, 25 Mar 2021 15:33:09 +0000 Subject: hw/arm/virt: Only try to add valid dynamic sysbus devices to platform bus The virt machine device plug callback currently calls platform_bus_link_device() for any sysbus device. This is overly broad, because platform_bus_link_device() will unconditionally grab the IRQs and MMIOs of the device it is passed, whether it was intended for the platform bus or not. Restrict hotpluggability of sysbus devices to only those devices on the dynamic sysbus allowlist. We were mostly getting away with this because the board creates the platform bus as the last device it creates, and so the hotplug callback did not do anything for all the sysbus devices created by the board itself. However if the user plugged in a device which itself uses a sysbus device internally we would have mishandled this and probably asserted. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Reviewed-by: Mark Cave-Ayland Reviewed-by: Eric Auger Message-id: 20210325153310.9131-4-peter.maydell@linaro.org --- hw/arm/virt.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'hw') diff --git a/hw/arm/virt.c b/hw/arm/virt.c index aa2bbd1..8625152 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -2443,7 +2443,9 @@ static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev, VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); if (vms->platform_bus_dev) { - if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) { + MachineClass *mc = MACHINE_GET_CLASS(vms); + + if (device_is_dynamic_sysbus(mc, dev)) { platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev), SYS_BUS_DEVICE(dev)); } @@ -2527,7 +2529,9 @@ static void virt_machine_device_unplug_cb(HotplugHandler *hotplug_dev, static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine, DeviceState *dev) { - if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) || + MachineClass *mc = MACHINE_GET_CLASS(machine); + + if (device_is_dynamic_sysbus(mc, dev) || (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM))) { return HOTPLUG_HANDLER(machine); } -- cgit v1.1 From e7e0d52dc6cbce267757697f9730d5d8d2a916c1 Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Thu, 25 Mar 2021 15:33:10 +0000 Subject: hw/ppc/e500plat: Only try to add valid dynamic sysbus devices to platform bus The e500plat machine device plug callback currently calls platform_bus_link_device() for any sysbus device. This is overly broad, because platform_bus_link_device() will unconditionally grab the IRQs and MMIOs of the device it is passed, whether it was intended for the platform bus or not. Restrict hotpluggability of sysbus devices to only those devices on the dynamic sysbus allowlist. We were mostly getting away with this because the board creates the platform bus as the last device it creates, and so the hotplug callback did not do anything for all the sysbus devices created by the board itself. However if the user plugged in a device which itself uses a sysbus device internally we would have mishandled this and probably asserted. An example of this is: qemu-system-ppc64 -M ppce500 -device macio-oldworld This isn't a sensible command because the macio-oldworld device is really specific to the 'g3beige' machine, but we now fail with a reasonable error message rather than asserting: qemu-system-ppc64: Device heathrow is not supported by this machine yet. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Reviewed-by: Mark Cave-Ayland Reviewed-by: Eric Auger Acked-by: David Gibson Message-id: 20210325153310.9131-5-peter.maydell@linaro.org --- hw/ppc/e500plat.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'hw') diff --git a/hw/ppc/e500plat.c b/hw/ppc/e500plat.c index bddd5e7..fc911bb 100644 --- a/hw/ppc/e500plat.c +++ b/hw/ppc/e500plat.c @@ -48,7 +48,9 @@ static void e500plat_machine_device_plug_cb(HotplugHandler *hotplug_dev, PPCE500MachineState *pms = PPCE500_MACHINE(hotplug_dev); if (pms->pbus_dev) { - if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) { + MachineClass *mc = MACHINE_GET_CLASS(pms); + + if (device_is_dynamic_sysbus(mc, dev)) { platform_bus_link_device(pms->pbus_dev, SYS_BUS_DEVICE(dev)); } } @@ -58,7 +60,9 @@ static HotplugHandler *e500plat_machine_get_hotpug_handler(MachineState *machine, DeviceState *dev) { - if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) { + MachineClass *mc = MACHINE_GET_CLASS(machine); + + if (device_is_dynamic_sysbus(mc, dev)) { return HOTPLUG_HANDLER(machine); } -- cgit v1.1