From f7ebda3bf19eda277765a2b2d37332b325889f60 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 8 Mar 2023 03:16:51 +0200 Subject: [PATCH] Kernel: Move interrupt related functions to InterruptController --- kernel/include/kernel/InterruptController.h | 9 ++++++++- kernel/kernel/InterruptController.cpp | 19 +++++++++++++++++++ kernel/kernel/Scheduler.cpp | 14 +++----------- kernel/kernel/kernel.cpp | 3 --- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/kernel/include/kernel/InterruptController.h b/kernel/include/kernel/InterruptController.h index 6890ea266b..9ea6cd205c 100644 --- a/kernel/include/kernel/InterruptController.h +++ b/kernel/include/kernel/InterruptController.h @@ -2,6 +2,9 @@ #include +#define DISABLE_INTERRUPTS() asm volatile("cli") +#define ENABLE_INTERRUPTS() asm volatile("sti") + class InterruptController { public: @@ -13,4 +16,8 @@ public: static void initialize(bool force_pic); static InterruptController& get(); -}; \ No newline at end of file +}; + +uintptr_t disable_interrupts_and_get_flags(); +void restore_flags(uintptr_t); +bool interrupts_enabled(); \ No newline at end of file diff --git a/kernel/kernel/InterruptController.cpp b/kernel/kernel/InterruptController.cpp index 50e451a2ce..fadff7ec4c 100644 --- a/kernel/kernel/InterruptController.cpp +++ b/kernel/kernel/InterruptController.cpp @@ -24,4 +24,23 @@ void InterruptController::initialize(bool force_pic) return; dprintln("Using PIC instead of APIC"); s_instance = PIC::create(); +} + +uintptr_t disable_interrupts_and_get_flags() +{ + uintptr_t flags; + asm volatile("pushf; cli; pop %0" : "=r"(flags) :: "memory"); + return flags; +} + +void restore_flags(uintptr_t flags) +{ + asm volatile("push %0; popf" :: "rm"(flags) : "memory", "cc"); +} + +bool interrupts_enabled() +{ + uintptr_t flags; + asm volatile("pushf; pop %0" : "=r"(flags) :: "memory"); + return flags & (1 << 9); } \ No newline at end of file diff --git a/kernel/kernel/Scheduler.cpp b/kernel/kernel/Scheduler.cpp index 0c805517a0..bc7d5beeb8 100644 --- a/kernel/kernel/Scheduler.cpp +++ b/kernel/kernel/Scheduler.cpp @@ -5,12 +5,11 @@ #include -#define DISABLE_INTERRUPTS() asm volatile("cli") -#define ENABLE_INTERRUPTS() asm volatile("sti") - #if 1 - #define VERIFY_CLI() ASSERT(interrupts_disabled()) + #define VERIFY_STI() ASSERT(interrupts_enabled()) + #define VERIFY_CLI() ASSERT(!interrupts_enabled()) #else + #define VERIFY_STI() #define VERIFY_CLI() #endif @@ -23,13 +22,6 @@ namespace Kernel static Scheduler* s_instance = nullptr; - static bool interrupts_disabled() - { - uintptr_t flags; - asm volatile("pushf; pop %0" : "=r"(flags)); - return !(flags & (1 << 9)); - } - BAN::ErrorOr Scheduler::initialize() { ASSERT(s_instance == nullptr); diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index 6b1f26c161..9b50c3d1e8 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -17,9 +17,6 @@ #include #include -#define DISABLE_INTERRUPTS() asm volatile("cli") -#define ENABLE_INTERRUPTS() asm volatile("sti") - extern "C" const char g_kernel_cmdline[]; struct ParsedCommandLine