From 5f85f1abaac8d1a773a813048b53f23e26cf2193 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Tue, 17 Jan 2023 20:47:51 +0100 Subject: [PATCH] Kernel: Simplify (un)registering interrupt logic Lose a level of indentation and remove a superfluous `handler_slot` check. --- Kernel/Arch/aarch64/Interrupts.cpp | 63 ++++++++++++++---------------- Kernel/Arch/x86_64/Interrupts.cpp | 60 ++++++++++++++-------------- 2 files changed, 59 insertions(+), 64 deletions(-) diff --git a/Kernel/Arch/aarch64/Interrupts.cpp b/Kernel/Arch/aarch64/Interrupts.cpp index 398b17d53b..450053daad 100644 --- a/Kernel/Arch/aarch64/Interrupts.cpp +++ b/Kernel/Arch/aarch64/Interrupts.cpp @@ -64,48 +64,45 @@ static void revert_to_unused_handler(u8 interrupt_number) void register_generic_interrupt_handler(u8 interrupt_number, GenericInterruptHandler& handler) { auto*& handler_slot = s_interrupt_handlers[interrupt_number]; - if (handler_slot != nullptr) { - if (handler_slot->type() == HandlerType::UnhandledInterruptHandler) { - if (handler_slot) { - auto* unhandled_handler = static_cast(handler_slot); - unhandled_handler->unregister_interrupt_handler(); - delete unhandled_handler; - } - handler_slot = &handler; - return; - } - if (handler_slot->is_shared_handler() && !handler_slot->is_sharing_with_others()) { - VERIFY(handler_slot->type() == HandlerType::SharedIRQHandler); - static_cast(handler_slot)->register_handler(handler); - return; - } - if (!handler_slot->is_shared_handler()) { - if (handler_slot->type() == HandlerType::SpuriousInterruptHandler) { - // FIXME: Add support for spurious interrupts on aarch64 - TODO_AARCH64(); - } - VERIFY(handler_slot->type() == HandlerType::IRQHandler); - auto& previous_handler = *handler_slot; - handler_slot = nullptr; - SharedIRQHandler::initialize(interrupt_number); - VERIFY(handler_slot); - static_cast(handler_slot)->register_handler(previous_handler); - static_cast(handler_slot)->register_handler(handler); - return; - } - VERIFY_NOT_REACHED(); - } else { + if (handler_slot == nullptr) { handler_slot = &handler; + return; } + if (handler_slot->type() == HandlerType::UnhandledInterruptHandler) { + auto* unhandled_handler = static_cast(handler_slot); + unhandled_handler->unregister_interrupt_handler(); + delete unhandled_handler; + handler_slot = &handler; + return; + } + if (handler_slot->is_shared_handler() && !handler_slot->is_sharing_with_others()) { + VERIFY(handler_slot->type() == HandlerType::SharedIRQHandler); + static_cast(handler_slot)->register_handler(handler); + return; + } + if (!handler_slot->is_shared_handler()) { + if (handler_slot->type() == HandlerType::SpuriousInterruptHandler) { + // FIXME: Add support for spurious interrupts on aarch64 + TODO_AARCH64(); + } + VERIFY(handler_slot->type() == HandlerType::IRQHandler); + auto& previous_handler = *handler_slot; + handler_slot = nullptr; + SharedIRQHandler::initialize(interrupt_number); + VERIFY(handler_slot); + static_cast(handler_slot)->register_handler(previous_handler); + static_cast(handler_slot)->register_handler(handler); + return; + } + VERIFY_NOT_REACHED(); } void unregister_generic_interrupt_handler(u8 interrupt_number, GenericInterruptHandler& handler) { auto*& handler_slot = s_interrupt_handlers[interrupt_number]; VERIFY(handler_slot != nullptr); - if (handler_slot->type() == HandlerType::UnhandledInterruptHandler) { + if (handler_slot->type() == HandlerType::UnhandledInterruptHandler) return; - } if (handler_slot->is_shared_handler() && !handler_slot->is_sharing_with_others()) { VERIFY(handler_slot->type() == HandlerType::SharedIRQHandler); auto* shared_handler = static_cast(handler_slot); diff --git a/Kernel/Arch/x86_64/Interrupts.cpp b/Kernel/Arch/x86_64/Interrupts.cpp index 7346163493..78d77c3285 100644 --- a/Kernel/Arch/x86_64/Interrupts.cpp +++ b/Kernel/Arch/x86_64/Interrupts.cpp @@ -478,39 +478,37 @@ void register_generic_interrupt_handler(u8 interrupt_number, GenericInterruptHan { VERIFY(interrupt_number < GENERIC_INTERRUPT_HANDLERS_COUNT); auto*& handler_slot = s_interrupt_handler[interrupt_number]; - if (handler_slot != nullptr) { - if (handler_slot->type() == HandlerType::UnhandledInterruptHandler) { - if (handler_slot) { - auto* unhandled_handler = static_cast(handler_slot); - unhandled_handler->unregister_interrupt_handler(); - delete unhandled_handler; - } - handler_slot = &handler; - return; - } - if (handler_slot->is_shared_handler() && !handler_slot->is_sharing_with_others()) { - VERIFY(handler_slot->type() == HandlerType::SharedIRQHandler); - static_cast(handler_slot)->register_handler(handler); - return; - } - if (!handler_slot->is_shared_handler()) { - if (handler_slot->type() == HandlerType::SpuriousInterruptHandler) { - static_cast(handler_slot)->register_handler(handler); - return; - } - VERIFY(handler_slot->type() == HandlerType::IRQHandler); - auto& previous_handler = *handler_slot; - handler_slot = nullptr; - SharedIRQHandler::initialize(interrupt_number); - VERIFY(handler_slot); - static_cast(handler_slot)->register_handler(previous_handler); - static_cast(handler_slot)->register_handler(handler); - return; - } - VERIFY_NOT_REACHED(); - } else { + if (handler_slot == nullptr) { handler_slot = &handler; + return; } + if (handler_slot->type() == HandlerType::UnhandledInterruptHandler) { + auto* unhandled_handler = static_cast(handler_slot); + unhandled_handler->unregister_interrupt_handler(); + delete unhandled_handler; + handler_slot = &handler; + return; + } + if (handler_slot->is_shared_handler() && !handler_slot->is_sharing_with_others()) { + VERIFY(handler_slot->type() == HandlerType::SharedIRQHandler); + static_cast(handler_slot)->register_handler(handler); + return; + } + if (!handler_slot->is_shared_handler()) { + if (handler_slot->type() == HandlerType::SpuriousInterruptHandler) { + static_cast(handler_slot)->register_handler(handler); + return; + } + VERIFY(handler_slot->type() == HandlerType::IRQHandler); + auto& previous_handler = *handler_slot; + handler_slot = nullptr; + SharedIRQHandler::initialize(interrupt_number); + VERIFY(handler_slot); + static_cast(handler_slot)->register_handler(previous_handler); + static_cast(handler_slot)->register_handler(handler); + return; + } + VERIFY_NOT_REACHED(); } void unregister_generic_interrupt_handler(u8 interrupt_number, GenericInterruptHandler& handler)