diff --git a/Kernel/Arch/i386/APIC.cpp b/Kernel/Arch/i386/APIC.cpp deleted file mode 100644 index 414763c6e9..0000000000 --- a/Kernel/Arch/i386/APIC.cpp +++ /dev/null @@ -1,220 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include - -#define IRQ_APIC_SPURIOUS 0x1f - -#define APIC_BASE_MSR 0x1b - -#define APIC_REG_LD 0xd0 -#define APIC_REG_DF 0xe0 -#define APIC_REG_SIV 0xf0 -#define APIC_REG_ICR_LOW 0x300 -#define APIC_REG_ICR_HIGH 0x310 -#define APIC_REG_LVT_TIMER 0x320 -#define APIC_REG_LVT_THERMAL 0x330 -#define APIC_REG_LVT_PERFORMANCE_COUNTER 0x340 -#define APIC_REG_LVT_LINT0 0x350 -#define APIC_REG_LVT_LINT1 0x360 -#define APIC_REG_LVT_ERR 0x370 - -namespace Kernel { - -extern "C" void apic_spurious_interrupt_entry(); - -asm( - ".globl apic_spurious_interrupt_entry \n" - "apic_spurious_interrupt_entry: \n" - " iret\n"); - -namespace APIC { - -class ICRReg { - u32 m_reg { 0 }; - -public: - enum DeliveryMode { - Fixed = 0x0, - LowPriority = 0x1, - SMI = 0x2, - NMI = 0x4, - INIT = 0x5, - StartUp = 0x6, - }; - enum DestinationMode { - Physical = 0x0, - Logical = 0x0, - }; - enum Level { - DeAssert = 0x0, - Assert = 0x1 - }; - enum class TriggerMode { - Edge = 0x0, - Level = 0x1, - }; - enum DestinationShorthand { - NoShorthand = 0x0, - Self = 0x1, - AllIncludingSelf = 0x2, - AllExcludingSelf = 0x3, - }; - - ICRReg(u8 vector, DeliveryMode delivery_mode, DestinationMode destination_mode, Level level, TriggerMode trigger_mode, DestinationShorthand destination) - : m_reg(vector | (delivery_mode << 8) | (destination_mode << 11) | (level << 14) | (static_cast(trigger_mode) << 15) | (destination << 18)) - { - } - - u32 low() const { return m_reg; } - u32 high() const { return 0; } -}; - -static volatile u8* g_apic_base = nullptr; - -static PhysicalAddress get_base() -{ - u32 lo, hi; - MSR msr(APIC_BASE_MSR); - msr.get(lo, hi); - return PhysicalAddress(lo & 0xfffff000); -} - -static void set_base(const PhysicalAddress& base) -{ - u32 hi = 0; - u32 lo = base.get() | 0x800; - MSR msr(APIC_BASE_MSR); - msr.set(lo, hi); -} - -static u32 apic_read(u32 off) -{ - return *(volatile u32*)(&g_apic_base[off]); -} - -static void apic_write(u32 off, u32 val) -{ - *reinterpret_cast(&g_apic_base[off]) = val; -} - -static void apic_write_icr(const ICRReg& icr) -{ - apic_write(APIC_REG_ICR_HIGH, icr.high()); - apic_write(APIC_REG_ICR_LOW, icr.low()); -} - -#define APIC_LVT_MASKED (1 << 15) -#define APIC_LVT_TRIGGER_LEVEL (1 << 14) -#define APIC_LVT(iv, dm) ((iv & 0xff) | ((dm & 0x7) << 8)) - -asm( - ".globl apic_ap_start \n" - ".type apic_ap_start, @function \n" - "apic_ap_start: \n" - ".set begin_apic_ap_start, . \n" - " jmp apic_ap_start\n" // TODO: implement - ".set end_apic_ap_start, . \n" - "\n" - ".globl apic_ap_start_size \n" - "apic_ap_start_size: \n" - ".word end_apic_ap_start - begin_apic_ap_start \n"); - -extern "C" void apic_ap_start(void); -extern "C" u16 apic_ap_start_size; - -bool init() -{ - // FIXME: This code is broken and therefore isn't called. Please map everything correctly before calling this code. - ASSERT_NOT_REACHED(); - if (!MSR::have()) - return false; - - // check if we support local apic - CPUID id(1); - if ((id.edx() & (1 << 9)) == 0) - return false; - - PhysicalAddress apic_base = get_base(); - kprintf("Initializing APIC, base: P%x\n", apic_base); - set_base(apic_base); - - g_apic_base = apic_base.as_ptr(); - - // copy ap init code to P8000 - memcpy(reinterpret_cast(0x8000), reinterpret_cast(apic_ap_start), apic_ap_start_size); - return true; -} - -void enable(u32 cpu) -{ - kprintf("Enabling local APIC for cpu #%u\n", cpu); - - // set spurious interrupt vector - apic_write(APIC_REG_SIV, apic_read(APIC_REG_SIV) | 0x100); - - // local destination mode (flat mode) - apic_write(APIC_REG_DF, 0xf000000); - - // set destination id (note that this limits it to 8 cpus) - apic_write(APIC_REG_LD, (1 << cpu) << 24); - - register_interrupt_handler(IRQ_APIC_SPURIOUS, apic_spurious_interrupt_entry); - - apic_write(APIC_REG_LVT_TIMER, APIC_LVT(0xff, 0) | APIC_LVT_MASKED); - apic_write(APIC_REG_LVT_THERMAL, APIC_LVT(0xff, 0) | APIC_LVT_MASKED); - apic_write(APIC_REG_LVT_PERFORMANCE_COUNTER, APIC_LVT(0xff, 0) | APIC_LVT_MASKED); - apic_write(APIC_REG_LVT_LINT0, APIC_LVT(0x1f, 7) | APIC_LVT_MASKED); - apic_write(APIC_REG_LVT_LINT1, APIC_LVT(0xff, 4) | APIC_LVT_TRIGGER_LEVEL); // nmi - apic_write(APIC_REG_LVT_ERR, APIC_LVT(0xe3, 0) | APIC_LVT_MASKED); - - if (cpu == 0) { - static volatile u32 foo = 0; - - // INIT - apic_write_icr(ICRReg(0, ICRReg::INIT, ICRReg::Physical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::AllExcludingSelf)); - - for (foo = 0; foo < 0x800000; foo++) - ; // TODO: 10 millisecond delay - - for (int i = 0; i < 2; i++) { - // SIPI - apic_write_icr(ICRReg(0x08, ICRReg::StartUp, ICRReg::Physical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::AllExcludingSelf)); // start execution at P8000 - - for (foo = 0; foo < 0x80000; foo++) - ; // TODO: 200 microsecond delay - } - } -} - -} - -} diff --git a/Kernel/Arch/i386/APIC.h b/Kernel/Arch/i386/APIC.h deleted file mode 100644 index 271c6445bf..0000000000 --- a/Kernel/Arch/i386/APIC.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -namespace Kernel { - -namespace APIC { - -bool init(); -void enable(u32 cpu); - -} - -} diff --git a/Kernel/Arch/i386/PIC.cpp b/Kernel/Arch/i386/PIC.cpp deleted file mode 100644 index 6d4d7a713a..0000000000 --- a/Kernel/Arch/i386/PIC.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -namespace Kernel { - -// The slave 8259 is connected to the master's IRQ2 line. -// This is really only to enhance clarity. -#define SLAVE_INDEX 2 - -#define PIC0_CTL 0x20 -#define PIC0_CMD 0x21 -#define PIC1_CTL 0xA0 -#define PIC1_CMD 0xA1 - -#ifdef DEBUG_PIC -static bool initialized; -#endif - -namespace PIC { - -void disable(u8 irq) -{ - InterruptDisabler disabler; - u8 imr; - if (irq & 8) { - imr = IO::in8(PIC1_CMD); - imr |= 1 << (irq - 8); - IO::out8(PIC1_CMD, imr); - } else { - imr = IO::in8(PIC0_CMD); - imr |= 1 << irq; - IO::out8(PIC0_CMD, imr); - } -} - -void enable(u8 irq) -{ - InterruptDisabler disabler; - u8 imr; - if (irq & 8) { - imr = IO::in8(PIC1_CMD); - imr &= ~(1 << (irq - 8)); - IO::out8(PIC1_CMD, imr); - } else { - imr = IO::in8(PIC0_CMD); - imr &= ~(1 << irq); - IO::out8(PIC0_CMD, imr); - } -} - -void eoi(u8 irq) -{ - if (irq & 8) - IO::out8(PIC1_CTL, 0x20); - IO::out8(PIC0_CTL, 0x20); -} - -void initialize() -{ -#ifdef DEBUG_PIC - ASSERT(!initialized); -#endif - - /* ICW1 (edge triggered mode, cascading controllers, expect ICW4) */ - IO::out8(PIC0_CTL, 0x11); - IO::out8(PIC1_CTL, 0x11); - - /* ICW2 (upper 5 bits specify ISR indices, lower 3 idunno) */ - IO::out8(PIC0_CMD, IRQ_VECTOR_BASE); - IO::out8(PIC1_CMD, IRQ_VECTOR_BASE + 0x08); - - /* ICW3 (configure master/slave relationship) */ - IO::out8(PIC0_CMD, 1 << SLAVE_INDEX); - IO::out8(PIC1_CMD, SLAVE_INDEX); - - /* ICW4 (set x86 mode) */ - IO::out8(PIC0_CMD, 0x01); - IO::out8(PIC1_CMD, 0x01); - - // Mask -- start out with all IRQs disabled. - IO::out8(PIC0_CMD, 0xff); - IO::out8(PIC1_CMD, 0xff); - - // ...except IRQ2, since that's needed for the master to let through slave interrupts. - enable(2); - - kprintf("PIC(i8259): cascading mode, vectors 0x%b-0x%b\n", IRQ_VECTOR_BASE, IRQ_VECTOR_BASE + 0x08); - -#ifdef DEBUG_PIC - initialized = true; -#endif -} - -u16 get_isr() -{ - IO::out8(PIC0_CTL, 0x0b); - IO::out8(PIC1_CTL, 0x0b); - u8 isr0 = IO::in8(PIC0_CTL); - u8 isr1 = IO::in8(PIC1_CTL); - return (isr1 << 8) | isr0; -} - -u16 get_irr() -{ - IO::out8(PIC0_CTL, 0x0a); - IO::out8(PIC1_CTL, 0x0a); - u8 irr0 = IO::in8(PIC0_CTL); - u8 irr1 = IO::in8(PIC1_CTL); - return (irr1 << 8) | irr0; -} - -} - -} diff --git a/Kernel/Arch/i386/PIC.h b/Kernel/Arch/i386/PIC.h deleted file mode 100644 index b6f013972c..0000000000 --- a/Kernel/Arch/i386/PIC.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -namespace Kernel { - -namespace PIC { - - void enable(u8 number); - void disable(u8 number); - void eoi(u8 number); - void initialize(); - u16 get_isr(); - u16 get_irr(); - -} - -class IRQHandlerScope { -public: - explicit IRQHandlerScope(u8 irq) - : m_irq(irq) - { - } - ~IRQHandlerScope() { PIC::eoi(m_irq); } - -private: - u8 m_irq { 0 }; -}; - -} diff --git a/Kernel/Arch/i386/PIT.cpp b/Kernel/Arch/i386/PIT.cpp deleted file mode 100644 index c74576747b..0000000000 --- a/Kernel/Arch/i386/PIT.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -namespace Kernel { - -#define IRQ_TIMER 0 - -extern "C" void timer_interrupt_entry(); -extern "C" void timer_interrupt_handler(RegisterState); - -asm( - ".globl timer_interrupt_entry \n" - "timer_interrupt_entry: \n" - " pushl $0x0\n" - " pusha\n" - " pushl %ds\n" - " pushl %es\n" - " pushl %fs\n" - " pushl %gs\n" - " pushl %ss\n" - " mov $0x10, %ax\n" - " mov %ax, %ds\n" - " mov %ax, %es\n" - " cld\n" - " call timer_interrupt_handler\n" - " add $0x4, %esp\n" - " popl %gs\n" - " popl %fs\n" - " popl %es\n" - " popl %ds\n" - " popa\n" - " add $0x4, %esp\n" - " iret\n"); - -static u32 s_ticks_this_second; -static u32 s_seconds_since_boot; - -void timer_interrupt_handler(RegisterState regs) -{ - clac(); - ++g_in_irq; - IRQHandlerScope scope(IRQ_TIMER); - if (++s_ticks_this_second >= TICKS_PER_SECOND) { - // FIXME: Synchronize with the RTC somehow to prevent drifting apart. - ++s_seconds_since_boot; - s_ticks_this_second = 0; - } - Scheduler::timer_tick(regs); - --g_in_irq; -} - -namespace PIT { - -u32 ticks_this_second() -{ - return s_ticks_this_second; -} - -u32 seconds_since_boot() -{ - return s_seconds_since_boot; -} - -void initialize() -{ - u16 timer_reload; - - IO::out8(PIT_CTL, TIMER0_SELECT | WRITE_WORD | MODE_SQUARE_WAVE); - - timer_reload = (BASE_FREQUENCY / TICKS_PER_SECOND); - - kprintf("PIT: %u Hz, square wave (%x)\n", TICKS_PER_SECOND, timer_reload); - - IO::out8(TIMER0_CTL, LSB(timer_reload)); - IO::out8(TIMER0_CTL, MSB(timer_reload)); - - register_interrupt_handler(IRQ_VECTOR_BASE + IRQ_TIMER, timer_interrupt_entry); - - PIC::enable(IRQ_TIMER); -} - -} - -} diff --git a/Kernel/Arch/i386/PIT.h b/Kernel/Arch/i386/PIT.h deleted file mode 100644 index b2fed1b3ac..0000000000 --- a/Kernel/Arch/i386/PIT.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -namespace Kernel { - -#define TICKS_PER_SECOND 1000 -/* Timer related ports */ -#define TIMER0_CTL 0x40 -#define TIMER1_CTL 0x41 -#define TIMER2_CTL 0x42 -#define PIT_CTL 0x43 - -/* Building blocks for PIT_CTL */ -#define TIMER0_SELECT 0x00 -#define TIMER1_SELECT 0x40 -#define TIMER2_SELECT 0x80 - -#define MODE_COUNTDOWN 0x00 -#define MODE_ONESHOT 0x02 -#define MODE_RATE 0x04 -#define MODE_SQUARE_WAVE 0x06 - -#define WRITE_WORD 0x30 - -#define BASE_FREQUENCY 1193182 - -namespace PIT { - -void initialize(); -u32 ticks_this_second(); -u32 seconds_since_boot(); - -} - -} diff --git a/Kernel/IRQHandler.cpp b/Kernel/IRQHandler.cpp deleted file mode 100644 index 4404b1c723..0000000000 --- a/Kernel/IRQHandler.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "IRQHandler.h" -#include -#include - -namespace Kernel { - -IRQHandler::IRQHandler(u8 irq) - : m_irq_number(irq) -{ - register_irq_handler(m_irq_number, *this); -} - -IRQHandler::~IRQHandler() -{ - unregister_irq_handler(m_irq_number, *this); -} - -void IRQHandler::enable_irq() -{ - PIC::enable(m_irq_number); -} - -void IRQHandler::disable_irq() -{ - PIC::disable(m_irq_number); -} - -} diff --git a/Kernel/IRQHandler.h b/Kernel/IRQHandler.h deleted file mode 100644 index 6262897780..0000000000 --- a/Kernel/IRQHandler.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -namespace Kernel { - -class IRQHandler { -public: - virtual ~IRQHandler(); - virtual void handle_irq() = 0; - - u8 irq_number() const { return m_irq_number; } - - void enable_irq(); - void disable_irq(); - -protected: - explicit IRQHandler(u8 irq); - -private: - u8 m_irq_number { 0 }; -}; - -} diff --git a/Kernel/InterruptHandler.cpp b/Kernel/InterruptHandler.cpp deleted file mode 100644 index 0d30ce94d1..0000000000 --- a/Kernel/InterruptHandler.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include - -InterruptHandler::InterruptHandler(u8 irq) - : m_irq_number(irq) -{ - SharedInterruptHandler::from(m_irq_number).register_handler(*this); - disable_interrupts(); -} - -InterruptHandler::~InterruptHandler() -{ - disable_interrupts(); - SharedInterruptHandler::from(m_irq_number).unregister_handler(*this); -} - -void InterruptHandler::enable_interrupts() -{ - m_enabled = true; -} - -void InterruptHandler::change_irq_number(u8 irq_number) -{ - bool was_enabled = m_enabled; - disable_interrupts(); - SharedInterruptHandler::from(m_irq_number).unregister_handler(*this); - m_irq_number = irq_number; - SharedInterruptHandler::from(m_irq_number).register_handler(*this); - if (was_enabled) - enable_interrupts(); -} - -void InterruptHandler::disable_interrupts() -{ - m_enabled = false; -} - -InterruptHandler::Enabler::Enabler(InterruptHandler& handler) - : m_handler(handler) - , m_was_enabled(m_handler.is_enabled()) -{ - m_handler.enable_interrupts(); -} -InterruptHandler::Enabler::~Enabler() -{ - if (!m_was_enabled) - m_handler.disable_interrupts(); -} diff --git a/Kernel/InterruptHandler.h b/Kernel/InterruptHandler.h deleted file mode 100644 index 45e20b92a8..0000000000 --- a/Kernel/InterruptHandler.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -class InterruptHandler { -public: - class Enabler { - public: - explicit Enabler(InterruptHandler&); - ~Enabler(); - - private: - InterruptHandler& m_handler; - bool m_was_enabled; - }; - friend class Enabler; - -public: - virtual ~InterruptHandler(); - virtual void handle_interrupt() = 0; - - u8 irq_number() const { return m_irq_number; } - bool is_enabled() const { return m_enabled; } - -protected: - void enable_interrupts(); - void change_irq_number(u8 irq_number); - void disable_interrupts(); - explicit InterruptHandler(u8 irq); - -private: - bool m_enabled { false }; - u8 m_irq_number { 0 }; -}; diff --git a/Kernel/SharedInterruptHandler.cpp b/Kernel/SharedInterruptHandler.cpp deleted file mode 100644 index b1f91ad523..0000000000 --- a/Kernel/SharedInterruptHandler.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include - -//#define INTERRUPT_DEBUG - -SharedInterruptHandler& SharedInterruptHandler::from(u8 interrupt_number) -{ - return get_interrupt_handler(interrupt_number); -} - -void SharedInterruptHandler::initialize(u8 interrupt_number) -{ - new SharedInterruptHandler(interrupt_number); -} - -void SharedInterruptHandler::register_handler(InterruptHandler& handler) -{ -#ifdef INTERRUPT_DEBUG - kprintf("Interrupt Handler registered @ Shared Interrupt Handler %d\n", m_interrupt_number); -#endif - m_handlers.set(&handler); - enable_interrupt_vector(); -} -void SharedInterruptHandler::unregister_handler(InterruptHandler& handler) -{ -#ifdef INTERRUPT_DEBUG - kprintf("Interrupt Handler unregistered @ Shared Interrupt Handler %d\n", m_interrupt_number); -#endif - m_handlers.remove(&handler); - if (m_handlers.is_empty()) - disable_interrupt_vector(); -} - -SharedInterruptHandler::SharedInterruptHandler(u8 interrupt_number) - : m_interrupt_number(interrupt_number) - , m_enabled(true) -{ -#ifdef INTERRUPT_DEBUG - kprintf("Shared Interrupt Handler registered @ %d\n", m_interrupt_number); -#endif - register_shared_interrupt_handler(m_interrupt_number, *this); - disable_interrupt_vector(); -} - -SharedInterruptHandler::~SharedInterruptHandler() -{ -#ifdef INTERRUPT_DEBUG - kprintf("Shared Interrupt Handler unregistered @ %d\n", m_interrupt_number); -#endif - disable_interrupt_vector(); - unregister_shared_interrupt_handler(m_interrupt_number, *this); -} - -void SharedInterruptHandler::handle_interrupt() -{ -#ifdef INTERRUPT_DEBUG - kprintf("Interrupt @ %d\n", m_interrupt_number); - kprintf("Interrupt Handlers registered - %d\n", m_handlers.size()); -#endif - int i = 0; - for (auto* handler : m_handlers) { -#ifdef INTERRUPT_DEBUG - kprintf("Going for Interrupt Handling @ %d, Shared Interrupt %d\n", i, m_interrupt_number); -#endif - ASSERT(handler != nullptr); - if (handler->is_enabled()) - handler->handle_interrupt(); - -#ifdef INTERRUPT_DEBUG - kprintf("Going for Interrupt Handling @ %d, Shared Interrupt %d - End\n", i, m_interrupt_number); -#endif - i++; - } - // FIXME: Determine if we use IRQs or MSIs (in the future) to send EOI... -} - -void SharedInterruptHandler::enable_interrupt_vector() -{ - if (m_enabled) - return; - m_enabled = true; - // FIXME: Determine if we use IRQs or MSIs (in the future) to enable the interrupt vector... - PIC::enable(m_interrupt_number); -} - -void SharedInterruptHandler::disable_interrupt_vector() -{ - if (!m_enabled) - return; - m_enabled = false; - // FIXME: Determine if we use IRQs or MSIs (in the future) to disable the interrupt vector... - PIC::disable(m_interrupt_number); -} diff --git a/Kernel/SharedInterruptHandler.h b/Kernel/SharedInterruptHandler.h deleted file mode 100644 index 701147c5c6..0000000000 --- a/Kernel/SharedInterruptHandler.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include -#include -#include - -class InterruptHandler; -class SharedInterruptHandler final { -public: - static void initialize(u8 interrupt_number); - static SharedInterruptHandler& from(u8 interrupt_number); - ~SharedInterruptHandler(); - void handle_interrupt(); - - u8 interrupt_number() const { return m_interrupt_number; } - - void register_handler(InterruptHandler&); - void unregister_handler(InterruptHandler&); - -private: - void enable_interrupt_vector(); - void disable_interrupt_vector(); - explicit SharedInterruptHandler(u8 interrupt_number); - HashTable m_handlers; - u8 m_interrupt_number { 0 }; - bool m_enabled { false }; -};