From bb4404df23294c39882bb1dd00958757dd3a9b92 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 21 Nov 2023 11:36:22 -0500 Subject: [PATCH] Add suspend_interrupts() and resume_interrupts() --- src/hulk/cpu.d | 31 +++++++++++++++++++++++++++++++ src/hulk/hulk.d | 6 ++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/hulk/cpu.d b/src/hulk/cpu.d index 827d2c2..ec51941 100644 --- a/src/hulk/cpu.d +++ b/src/hulk/cpu.d @@ -135,6 +135,37 @@ enum uint CPUID_1_ECX_RDRND = 0x4000_0000u; enum uint CPUID_1_ECX_HYPERVISOR = 0x8000_0000u; /** @} */ +/** Interrupt suspend level (0 when interrupts are enabled). */ +private __gshared size_t intr_suspend_level; + +/** + * Suspend interrupts. + * + * This function disables interrupts. Calls can be nested and interrupts will + * be enabled only when the outermost pair of + * suspend_interrupts()/resume_interrupts() is complete. + */ +void suspend_interrupts() +{ + cli(); + intr_suspend_level++; +} + +/** + * Resume interrupts. + * + * Calls can be nested and interrupts will be enabled only when the outermost + * pair of suspend_interrupts()/resume_interrupts() is complete. + */ +void resume_interrupts() +{ + intr_suspend_level--; + if (intr_suspend_level == 0) + { + sti(); + } +} + void cli() { __asm("cli", ""); diff --git a/src/hulk/hulk.d b/src/hulk/hulk.d index 916a2f5..cc1095d 100644 --- a/src/hulk/hulk.d +++ b/src/hulk/hulk.d @@ -58,7 +58,8 @@ private void initialize_cpu() */ void hulk_start() { - cli(); + suspend_interrupts(); + initialize_cpu(); Serial.initialize(); Gdt.initialize(); @@ -86,7 +87,8 @@ void hulk_start() Pit.initialize(); Pci.initialize(); Usb.initialize(); - sti(); + + resume_interrupts(); /* Check that PIT millisecond interrupt is firing. */ size_t uptime = Time.uptime();