1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:27:35 +00:00

Kernel/VirtIO: Defer initialization of device out of the constructor

This ensures we safely handle interrupts (which can call virtual
functions), so they don't happen in the constructor - this pattern can
lead to a crash, if we are still in the constructor context because
not all methods are available for usage (some are pure virtual,
so it's possible to call __cxa_pure_virtual).

Also, under some conditions like adding a PCI device via PCI-passthrough
mechanism in QEMU, it became exposed to the eye that the code asserts on
RNG::handle_device_config_change(). That device has no configuration but
if the hypervisor still misbehaves and tries to configure it, we should
simply return false to indicate nothing happened.
This commit is contained in:
Liav A 2021-09-04 08:42:31 +03:00 committed by Andreas Kling
parent e490c17bde
commit ed6c1f53af
9 changed files with 47 additions and 17 deletions

View file

@ -25,11 +25,13 @@ UNMAP_AFTER_INIT void detect()
return;
switch (id.device_id) {
case PCI::DeviceID::VirtIOConsole: {
[[maybe_unused]] auto& unused = Console::must_create(address).leak_ref();
auto& console = Console::must_create(address).leak_ref();
console.initialize();
break;
}
case PCI::DeviceID::VirtIOEntropy: {
[[maybe_unused]] auto& unused = RNG::must_create(address).leak_ref();
auto& rng = RNG::must_create(address).leak_ref();
rng.initialize();
break;
}
case PCI::DeviceID::VirtIOGPU: {
@ -60,13 +62,9 @@ StringView determine_device_class(const PCI::Address& address)
VERIFY_NOT_REACHED();
}
UNMAP_AFTER_INIT VirtIO::Device::Device(PCI::Address address)
: PCI::Device(address)
, IRQHandler(PCI::get_interrupt_line(address))
, m_io_base(IOAddress(PCI::get_BAR0(pci_address()) & ~1))
UNMAP_AFTER_INIT void Device::initialize()
{
dbgln("{}: Found @ {}", VirtIO::determine_device_class(address), pci_address());
auto address = pci_address();
enable_bus_mastering(pci_address());
PCI::enable_interrupt_line(pci_address());
enable_irq();
@ -116,6 +114,14 @@ UNMAP_AFTER_INIT VirtIO::Device::Device(PCI::Address address)
set_status_bit(DEVICE_STATUS_DRIVER);
}
UNMAP_AFTER_INIT VirtIO::Device::Device(PCI::Address address)
: PCI::Device(address)
, IRQHandler(PCI::get_interrupt_line(address))
, m_io_base(IOAddress(PCI::get_BAR0(pci_address()) & ~1))
{
dbgln("{}: Found @ {}", VirtIO::determine_device_class(address), pci_address());
}
auto Device::mapping_for_bar(u8 bar) -> MappedMMIO&
{
VERIFY(m_use_mmio);