mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:47:44 +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:
parent
e490c17bde
commit
ed6c1f53af
9 changed files with 47 additions and 17 deletions
|
@ -17,10 +17,9 @@ UNMAP_AFTER_INIT NonnullRefPtr<Console> Console::must_create(PCI::Address addres
|
||||||
return adopt_ref_if_nonnull(new Console(address)).release_nonnull();
|
return adopt_ref_if_nonnull(new Console(address)).release_nonnull();
|
||||||
}
|
}
|
||||||
|
|
||||||
UNMAP_AFTER_INIT Console::Console(PCI::Address address)
|
UNMAP_AFTER_INIT void Console::initialize()
|
||||||
: VirtIO::Device(address)
|
|
||||||
, m_device_id(next_device_id++)
|
|
||||||
{
|
{
|
||||||
|
Device::initialize();
|
||||||
if (auto cfg = get_config(ConfigurationType::Device)) {
|
if (auto cfg = get_config(ConfigurationType::Device)) {
|
||||||
bool success = negotiate_features([&](u64 supported_features) {
|
bool success = negotiate_features([&](u64 supported_features) {
|
||||||
u64 negotiated = 0;
|
u64 negotiated = 0;
|
||||||
|
@ -58,6 +57,12 @@ UNMAP_AFTER_INIT Console::Console(PCI::Address address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UNMAP_AFTER_INIT Console::Console(PCI::Address address)
|
||||||
|
: VirtIO::Device(address)
|
||||||
|
, m_device_id(next_device_id++)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
bool Console::handle_device_config_change()
|
bool Console::handle_device_config_change()
|
||||||
{
|
{
|
||||||
dbgln("VirtIO::Console: Handle device config change");
|
dbgln("VirtIO::Console: Handle device config change");
|
||||||
|
|
|
@ -28,6 +28,8 @@ public:
|
||||||
return m_device_id;
|
return m_device_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void initialize() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual StringView class_name() const override { return "VirtIOConsole"; }
|
virtual StringView class_name() const override { return "VirtIOConsole"; }
|
||||||
explicit Console(PCI::Address);
|
explicit Console(PCI::Address);
|
||||||
|
|
|
@ -25,11 +25,13 @@ UNMAP_AFTER_INIT void detect()
|
||||||
return;
|
return;
|
||||||
switch (id.device_id) {
|
switch (id.device_id) {
|
||||||
case PCI::DeviceID::VirtIOConsole: {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case PCI::DeviceID::VirtIOEntropy: {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case PCI::DeviceID::VirtIOGPU: {
|
case PCI::DeviceID::VirtIOGPU: {
|
||||||
|
@ -60,13 +62,9 @@ StringView determine_device_class(const PCI::Address& address)
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
UNMAP_AFTER_INIT VirtIO::Device::Device(PCI::Address address)
|
UNMAP_AFTER_INIT void Device::initialize()
|
||||||
: 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 address = pci_address();
|
||||||
|
|
||||||
enable_bus_mastering(pci_address());
|
enable_bus_mastering(pci_address());
|
||||||
PCI::enable_interrupt_line(pci_address());
|
PCI::enable_interrupt_line(pci_address());
|
||||||
enable_irq();
|
enable_irq();
|
||||||
|
@ -116,6 +114,14 @@ UNMAP_AFTER_INIT VirtIO::Device::Device(PCI::Address address)
|
||||||
set_status_bit(DEVICE_STATUS_DRIVER);
|
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&
|
auto Device::mapping_for_bar(u8 bar) -> MappedMMIO&
|
||||||
{
|
{
|
||||||
VERIFY(m_use_mmio);
|
VERIFY(m_use_mmio);
|
||||||
|
|
|
@ -90,6 +90,8 @@ class Device
|
||||||
public:
|
public:
|
||||||
virtual ~Device() override = default;
|
virtual ~Device() override = default;
|
||||||
|
|
||||||
|
virtual void initialize();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual StringView class_name() const = 0;
|
virtual StringView class_name() const = 0;
|
||||||
explicit Device(PCI::Address);
|
explicit Device(PCI::Address);
|
||||||
|
|
|
@ -14,9 +14,9 @@ UNMAP_AFTER_INIT NonnullRefPtr<RNG> RNG::must_create(PCI::Address address)
|
||||||
return adopt_ref_if_nonnull(new RNG(address)).release_nonnull();
|
return adopt_ref_if_nonnull(new RNG(address)).release_nonnull();
|
||||||
}
|
}
|
||||||
|
|
||||||
UNMAP_AFTER_INIT RNG::RNG(PCI::Address address)
|
UNMAP_AFTER_INIT void RNG::initialize()
|
||||||
: VirtIO::Device(address)
|
|
||||||
{
|
{
|
||||||
|
Device::initialize();
|
||||||
bool success = negotiate_features([&](auto) {
|
bool success = negotiate_features([&](auto) {
|
||||||
return 0;
|
return 0;
|
||||||
});
|
});
|
||||||
|
@ -33,9 +33,14 @@ UNMAP_AFTER_INIT RNG::RNG(PCI::Address address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UNMAP_AFTER_INIT RNG::RNG(PCI::Address address)
|
||||||
|
: VirtIO::Device(address)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
bool RNG::handle_device_config_change()
|
bool RNG::handle_device_config_change()
|
||||||
{
|
{
|
||||||
VERIFY_NOT_REACHED(); // Device has no config
|
return false; // Device has no config
|
||||||
}
|
}
|
||||||
|
|
||||||
void RNG::handle_queue_update(u16 queue_index)
|
void RNG::handle_queue_update(u16 queue_index)
|
||||||
|
|
|
@ -23,6 +23,8 @@ public:
|
||||||
virtual StringView purpose() const override { return class_name(); }
|
virtual StringView purpose() const override { return class_name(); }
|
||||||
virtual ~RNG() override = default;
|
virtual ~RNG() override = default;
|
||||||
|
|
||||||
|
virtual void initialize() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual StringView class_name() const override { return "VirtIOConsole"; }
|
virtual StringView class_name() const override { return "VirtIOConsole"; }
|
||||||
explicit RNG(PCI::Address);
|
explicit RNG(PCI::Address);
|
||||||
|
|
|
@ -15,10 +15,9 @@
|
||||||
|
|
||||||
namespace Kernel::Graphics::VirtIOGPU {
|
namespace Kernel::Graphics::VirtIOGPU {
|
||||||
|
|
||||||
GPU::GPU(PCI::Address address)
|
void GPU::initialize()
|
||||||
: VirtIO::Device(address)
|
|
||||||
, m_scratch_space(MM.allocate_contiguous_kernel_region(32 * PAGE_SIZE, "VirtGPU Scratch Space", Memory::Region::Access::ReadWrite))
|
|
||||||
{
|
{
|
||||||
|
Device::initialize();
|
||||||
VERIFY(!!m_scratch_space);
|
VERIFY(!!m_scratch_space);
|
||||||
if (auto cfg = get_config(VirtIO::ConfigurationType::Device)) {
|
if (auto cfg = get_config(VirtIO::ConfigurationType::Device)) {
|
||||||
m_device_configuration = cfg;
|
m_device_configuration = cfg;
|
||||||
|
@ -47,6 +46,12 @@ GPU::GPU(PCI::Address address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GPU::GPU(PCI::Address address)
|
||||||
|
: VirtIO::Device(address)
|
||||||
|
, m_scratch_space(MM.allocate_contiguous_kernel_region(32 * PAGE_SIZE, "VirtGPU Scratch Space", Memory::Region::Access::ReadWrite))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
GPU::~GPU()
|
GPU::~GPU()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,8 @@ public:
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void initialize() override;
|
||||||
|
|
||||||
RefPtr<Console> default_console()
|
RefPtr<Console> default_console()
|
||||||
{
|
{
|
||||||
if (m_default_scanout.has_value())
|
if (m_default_scanout.has_value())
|
||||||
|
|
|
@ -22,6 +22,7 @@ GraphicsAdapter::GraphicsAdapter(PCI::Address base_address)
|
||||||
: PCI::Device(base_address)
|
: PCI::Device(base_address)
|
||||||
{
|
{
|
||||||
m_gpu_device = adopt_ref(*new GPU(base_address)).leak_ref();
|
m_gpu_device = adopt_ref(*new GPU(base_address)).leak_ref();
|
||||||
|
m_gpu_device->initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphicsAdapter::initialize_framebuffer_devices()
|
void GraphicsAdapter::initialize_framebuffer_devices()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue