From bde3c7301e89bade65231a2f3d2a96eaa1da1e91 Mon Sep 17 00:00:00 2001 From: Liav A Date: Tue, 7 Sep 2021 07:29:11 +0300 Subject: Kernel/ACPI: Return Optional container after table search This is a better pattern than returning a PhysicalAddress with a zero value, so the code is more understandable now. --- Kernel/ACPI/Definitions.h | 2 +- Kernel/ACPI/Parser.cpp | 19 +++++++++---------- Kernel/ACPI/Parser.h | 2 +- Kernel/Bus/PCI/Initializer.cpp | 6 ++++-- Kernel/Interrupts/APIC.cpp | 4 ++-- Kernel/Interrupts/InterruptManagement.cpp | 2 +- Kernel/Time/HPET.cpp | 16 ++++++++-------- 7 files changed, 26 insertions(+), 25 deletions(-) diff --git a/Kernel/ACPI/Definitions.h b/Kernel/ACPI/Definitions.h index 748fe3abc2..2defd3d49c 100644 --- a/Kernel/ACPI/Definitions.h +++ b/Kernel/ACPI/Definitions.h @@ -330,7 +330,7 @@ class Parser; namespace StaticParsing { Optional find_rsdp(); -PhysicalAddress find_table(PhysicalAddress rsdp, const StringView& signature); +Optional find_table(PhysicalAddress rsdp, const StringView& signature); } } diff --git a/Kernel/ACPI/Parser.cpp b/Kernel/ACPI/Parser.cpp index 55be2b0d59..624ce602ef 100644 --- a/Kernel/ACPI/Parser.cpp +++ b/Kernel/ACPI/Parser.cpp @@ -103,8 +103,8 @@ void Parser::set_the(Parser& parser) } static bool match_table_signature(PhysicalAddress table_header, const StringView& signature); -static PhysicalAddress search_table_in_xsdt(PhysicalAddress xsdt, const StringView& signature); -static PhysicalAddress search_table_in_rsdt(PhysicalAddress rsdt, const StringView& signature); +static Optional search_table_in_xsdt(PhysicalAddress xsdt, const StringView& signature); +static Optional search_table_in_rsdt(PhysicalAddress rsdt, const StringView& signature); static bool validate_table(const Structures::SDTHeader&, size_t length); UNMAP_AFTER_INIT void Parser::locate_static_data() @@ -115,7 +115,7 @@ UNMAP_AFTER_INIT void Parser::locate_static_data() init_facs(); } -UNMAP_AFTER_INIT PhysicalAddress Parser::find_table(const StringView& signature) +UNMAP_AFTER_INIT Optional Parser::find_table(const StringView& signature) { dbgln_if(ACPI_DEBUG, "ACPI: Calling Find Table method!"); for (auto p_sdt : m_sdt_pointers) { @@ -131,7 +131,8 @@ UNMAP_AFTER_INIT PhysicalAddress Parser::find_table(const StringView& signature) UNMAP_AFTER_INIT void Parser::init_facs() { - m_facs = find_table("FACS"); + if (auto facs = find_table("FACS"); facs.has_value()) + m_facs = facs.value(); } UNMAP_AFTER_INIT void Parser::init_fadt() @@ -139,9 +140,7 @@ UNMAP_AFTER_INIT void Parser::init_fadt() dmesgln("ACPI: Initializing Fixed ACPI data"); dmesgln("ACPI: Searching for the Fixed ACPI Data Table"); - m_fadt = find_table("FACP"); - VERIFY(!m_fadt.is_null()); - + m_fadt = find_table("FACP").value(); auto sdt = Memory::map_typed(m_fadt); dbgln_if(ACPI_DEBUG, "ACPI: FADT @ V{}, {}", &sdt, m_fadt); @@ -379,7 +378,7 @@ UNMAP_AFTER_INIT Optional StaticParsing::find_rsdp() return map_bios().find_chunk_starting_with(signature, 16); } -UNMAP_AFTER_INIT PhysicalAddress StaticParsing::find_table(PhysicalAddress rsdp_address, const StringView& signature) +UNMAP_AFTER_INIT Optional StaticParsing::find_table(PhysicalAddress rsdp_address, const StringView& signature) { // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables. VERIFY(signature.length() == 4); @@ -397,7 +396,7 @@ UNMAP_AFTER_INIT PhysicalAddress StaticParsing::find_table(PhysicalAddress rsdp_ VERIFY_NOT_REACHED(); } -UNMAP_AFTER_INIT static PhysicalAddress search_table_in_xsdt(PhysicalAddress xsdt_address, const StringView& signature) +UNMAP_AFTER_INIT static Optional search_table_in_xsdt(PhysicalAddress xsdt_address, const StringView& signature) { // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables. VERIFY(signature.length() == 4); @@ -420,7 +419,7 @@ static bool match_table_signature(PhysicalAddress table_header, const StringView return !strncmp(table->h.sig, signature.characters_without_null_termination(), 4); } -UNMAP_AFTER_INIT static PhysicalAddress search_table_in_rsdt(PhysicalAddress rsdt_address, const StringView& signature) +UNMAP_AFTER_INIT static Optional search_table_in_rsdt(PhysicalAddress rsdt_address, const StringView& signature) { // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables. VERIFY(signature.length() == 4); diff --git a/Kernel/ACPI/Parser.h b/Kernel/ACPI/Parser.h index 3180da8375..190489bab5 100644 --- a/Kernel/ACPI/Parser.h +++ b/Kernel/ACPI/Parser.h @@ -48,7 +48,7 @@ public: set_the(*new ParserType(rsdp)); } - virtual PhysicalAddress find_table(const StringView& signature); + virtual Optional find_table(const StringView& signature); virtual void try_acpi_reboot(); virtual bool can_reboot(); diff --git a/Kernel/Bus/PCI/Initializer.cpp b/Kernel/Bus/PCI/Initializer.cpp index 4754ca3ff9..5fb1fb11b9 100644 --- a/Kernel/Bus/PCI/Initializer.cpp +++ b/Kernel/Bus/PCI/Initializer.cpp @@ -21,7 +21,7 @@ static bool test_pci_io(); UNMAP_AFTER_INIT static PCIAccessLevel detect_optimal_access_type(PCIAccessLevel boot_determined) { - if (!ACPI::is_enabled() || ACPI::Parser::the()->find_table("MCFG").is_null()) + if (!ACPI::is_enabled() || !ACPI::Parser::the()->find_table("MCFG").has_value()) return PCIAccessLevel::IOAddressing; if (boot_determined != PCIAccessLevel::IOAddressing) @@ -39,7 +39,9 @@ UNMAP_AFTER_INIT void initialize() switch (detect_optimal_access_type(boot_determined)) { case PCIAccessLevel::MemoryAddressing: { - auto success = Access::initialize_for_memory_access(ACPI::Parser::the()->find_table("MCFG")); + auto mcfg = ACPI::Parser::the()->find_table("MCFG"); + VERIFY(mcfg.has_value()); + auto success = Access::initialize_for_memory_access(mcfg.value()); VERIFY(success); break; } diff --git a/Kernel/Interrupts/APIC.cpp b/Kernel/Interrupts/APIC.cpp index d7c55b9382..98124015d7 100644 --- a/Kernel/Interrupts/APIC.cpp +++ b/Kernel/Interrupts/APIC.cpp @@ -265,12 +265,12 @@ UNMAP_AFTER_INIT bool APIC::init_bsp() return false; } auto madt_address = ACPI::StaticParsing::find_table(rsdp.value(), "APIC"); - if (madt_address.is_null()) { + if (!madt_address.has_value()) { dbgln("APIC: MADT table not found"); return false; } - auto madt = Memory::map_typed(madt_address); + auto madt = Memory::map_typed(madt_address.value()); size_t entry_index = 0; size_t entries_length = madt->h.length - sizeof(ACPI::Structures::MADT); auto* madt_entry = madt->entries; diff --git a/Kernel/Interrupts/InterruptManagement.cpp b/Kernel/Interrupts/InterruptManagement.cpp index d1bc0e8293..b1c6b9dae9 100644 --- a/Kernel/Interrupts/InterruptManagement.cpp +++ b/Kernel/Interrupts/InterruptManagement.cpp @@ -115,7 +115,7 @@ UNMAP_AFTER_INIT PhysicalAddress InterruptManagement::search_for_madt() auto rsdp = ACPI::StaticParsing::find_rsdp(); if (!rsdp.has_value()) return {}; - return ACPI::StaticParsing::find_table(rsdp.value(), "APIC"); + return ACPI::StaticParsing::find_table(rsdp.value(), "APIC").value(); } UNMAP_AFTER_INIT InterruptManagement::InterruptManagement() diff --git a/Kernel/Time/HPET.cpp b/Kernel/Time/HPET.cpp index 7014033e83..9b94806ecc 100644 --- a/Kernel/Time/HPET.cpp +++ b/Kernel/Time/HPET.cpp @@ -119,12 +119,12 @@ UNMAP_AFTER_INIT bool HPET::test_and_initialize() { VERIFY(!HPET::initialized()); hpet_initialized = true; - auto hpet = ACPI::Parser::the()->find_table("HPET"); - if (hpet.is_null()) + auto hpet_table = ACPI::Parser::the()->find_table("HPET"); + if (!hpet_table.has_value()) return false; - dmesgln("HPET @ {}", hpet); + dmesgln("HPET @ {}", hpet_table.value()); - auto sdt = Memory::map_typed(hpet); + auto sdt = Memory::map_typed(hpet_table.value()); // Note: HPET is only usable from System Memory VERIFY(sdt->event_timer_block.address_space == (u8)ACPI::GenericAddressStructure::AddressSpace::SystemMemory); @@ -135,17 +135,17 @@ UNMAP_AFTER_INIT bool HPET::test_and_initialize() return false; } } - new HPET(PhysicalAddress(hpet)); + new HPET(PhysicalAddress(hpet_table.value())); return true; } UNMAP_AFTER_INIT bool HPET::check_for_exisiting_periodic_timers() { - auto hpet = ACPI::Parser::the()->find_table("HPET"); - if (hpet.is_null()) + auto hpet_table = ACPI::Parser::the()->find_table("HPET"); + if (!hpet_table.has_value()) return false; - auto sdt = Memory::map_typed(hpet); + auto sdt = Memory::map_typed(hpet_table.value()); VERIFY(sdt->event_timer_block.address_space == 0); auto registers = Memory::map_typed(PhysicalAddress(sdt->event_timer_block.address)); -- cgit v1.2.3