pcihost: Fix pci_unregister_device.

Remove the device from the hosts dev_map.
Remove the device from the Machine object.
This commit is contained in:
joevt 2024-08-14 01:29:46 -07:00 committed by dingusdev
parent c9359592d7
commit 7a55a81186
3 changed files with 14 additions and 3 deletions

View File

@ -75,15 +75,17 @@ void PCIHost::pci_unregister_device(int dev_fun_num)
if (!this->dev_map.count(dev_fun_num)) {
return;
}
PCIBase* dev_instance = this->dev_map[dev_fun_num];
auto dev_instance = this->dev_map[dev_fun_num];
HWComponent *hwc = dynamic_cast<HWComponent*>(this);
LOG_F(
ERROR, "%s: pci_unregister_device(%s) not supported yet (every PCI device needs a working destructor)",
// FIXME: need destructors to remove memory regions and downstream devices.
ERROR, "%s: pci_unregister_device(%s) not supported yet (every PCI device needs a working destructor to unregister memory and downstream devices etc.)",
hwc ? hwc->get_name().c_str() : "PCIHost", dev_instance->get_name().c_str()
);
delete dev_instance;
this->dev_map.erase(dev_fun_num);
gMachineObj->remove_device(dev_instance); // calls destructor of dev_instance since it is a unique_ptr in the device_map.
}
bool PCIHost::pci_register_mmio_region(uint32_t start_addr, uint32_t size, PCIBase* obj)

View File

@ -49,6 +49,14 @@ void MachineBase::add_device(std::string name, std::unique_ptr<HWComponent> dev_
this->device_map[name] = std::move(dev_obj);
}
void MachineBase::remove_device(HWComponent* dev_obj) {
for (auto it = this->device_map.begin(); it != this->device_map.end(); ++it)
if (it->second.get() == dev_obj) {
this->device_map.erase(it->first);
break;
}
}
HWComponent* MachineBase::get_comp_by_name(std::string name) {
if (this->device_map.count(name)) {
return this->device_map[name].get();

View File

@ -40,6 +40,7 @@ public:
~MachineBase();
void add_device(std::string name, std::unique_ptr<HWComponent> dev_obj);
void remove_device(HWComponent* dev_obj);
HWComponent* get_comp_by_name(std::string name);
HWComponent* get_comp_by_name_optional(std::string name);
HWComponent* get_comp_by_type(HWCompType type);