From 0d934fc9917f4f788c7579d42a736a78aeaddde8 Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Sun, 7 Mar 2021 21:28:28 +0100 Subject: [PATCH] Kernel::CPU: Move headers into common directory Alot of code is shared between i386/i686/x86 and x86_64 and a lot probably will be used for compatability modes. So we start by moving the headers into one Directory. We will probalby be able to move some cpp files aswell. --- AK/NonnullRefPtr.h | 2 +- AK/RefPtr.h | 2 +- AK/Singleton.h | 2 +- AK/Weakable.h | 2 +- Kernel/Arch/i386/CPU.cpp | 20 +-- .../i386/{Interrupts.h => InterruptEntry.cpp} | 42 +---- Kernel/Arch/i386/ProcessorInfo.cpp | 4 +- Kernel/Arch/i386/SafeMem.cpp | 4 +- Kernel/Arch/{i386 => x86}/CPU.h | 88 +-------- .../{x86_64/CPU.h => x86/DescriptorTable.h} | 38 ++-- Kernel/Arch/{i386 => x86}/ISRStubs.h | 2 +- Kernel/Arch/x86/Interrupts.h | 52 ++++++ Kernel/Arch/x86/PageDirectory.h | 170 ++++++++++++++++++ Kernel/Arch/{i386 => x86}/ProcessorInfo.h | 0 Kernel/Arch/{i386 => x86}/SafeMem.h | 0 Kernel/Arch/x86/SmapDisabler.h | 2 +- Kernel/Arch/x86/TSS.h | 88 +++++++++ Kernel/CMakeLists.txt | 12 +- Kernel/Devices/FullDevice.cpp | 2 +- Kernel/Devices/KeyboardDevice.cpp | 2 +- Kernel/Devices/PCSpeaker.cpp | 2 +- Kernel/Devices/VMWareBackdoor.cpp | 2 +- Kernel/FileSystem/ProcFS.cpp | 4 +- Kernel/Heap/kmalloc.cpp | 2 +- Kernel/IO.h | 2 +- Kernel/Interrupts/APIC.cpp | 4 +- Kernel/Interrupts/GenericInterruptHandler.cpp | 2 +- Kernel/Interrupts/GenericInterruptHandler.h | 2 +- Kernel/Interrupts/IOAPIC.cpp | 2 +- Kernel/Interrupts/IRQHandler.cpp | 2 +- Kernel/Interrupts/IRQHandler.h | 2 +- Kernel/Interrupts/InterruptManagement.cpp | 2 +- Kernel/Interrupts/MSIHandler.h | 2 +- Kernel/Interrupts/PIC.cpp | 2 +- Kernel/Interrupts/SharedIRQHandler.cpp | 2 +- Kernel/Interrupts/SharedIRQHandler.h | 2 +- Kernel/Interrupts/SpuriousInterruptHandler.h | 2 +- Kernel/Interrupts/UnhandledInterruptHandler.h | 2 +- Kernel/Lock.h | 2 +- Kernel/Panic.cpp | 2 +- Kernel/Process.cpp | 2 +- Kernel/Random.cpp | 2 +- Kernel/Random.h | 2 +- Kernel/SpinLock.h | 2 +- Kernel/StdLib.cpp | 2 +- Kernel/Syscall.cpp | 2 +- Kernel/Syscalls/sysconf.cpp | 2 +- Kernel/TTY/VirtualConsole.cpp | 2 +- Kernel/Thread.cpp | 2 +- Kernel/Thread.h | 4 +- Kernel/ThreadTracer.cpp | 2 +- Kernel/Time/APICTimer.cpp | 2 +- Kernel/Time/PIT.cpp | 2 +- Kernel/Time/RTC.cpp | 2 +- Kernel/VM/MemoryManager.cpp | 2 +- Kernel/VM/MemoryManager.h | 2 +- Kernel/VM/PhysicalPage.h | 2 +- Kernel/VM/Range.cpp | 2 +- Kernel/VM/Region.h | 2 +- Kernel/init.cpp | 2 +- 60 files changed, 412 insertions(+), 208 deletions(-) rename Kernel/Arch/i386/{Interrupts.h => InterruptEntry.cpp} (67%) rename Kernel/Arch/{i386 => x86}/CPU.h (94%) rename Kernel/Arch/{x86_64/CPU.h => x86/DescriptorTable.h} (80%) rename Kernel/Arch/{i386 => x86}/ISRStubs.h (99%) create mode 100644 Kernel/Arch/x86/Interrupts.h create mode 100644 Kernel/Arch/x86/PageDirectory.h rename Kernel/Arch/{i386 => x86}/ProcessorInfo.h (100%) rename Kernel/Arch/{i386 => x86}/SafeMem.h (100%) create mode 100644 Kernel/Arch/x86/TSS.h diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index d2895475007..6cc13277a49 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -31,7 +31,7 @@ #include #include #ifdef KERNEL -# include +# include #endif namespace AK { diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 0c8bafd525f..2a1720f9ac8 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -33,7 +33,7 @@ #include #include #ifdef KERNEL -# include +# include #endif namespace AK { diff --git a/AK/Singleton.h b/AK/Singleton.h index 18797eb6286..fe60c7099f6 100644 --- a/AK/Singleton.h +++ b/AK/Singleton.h @@ -30,7 +30,7 @@ #include #include #ifdef KERNEL -# include +# include #endif #ifndef __serenity__ diff --git a/AK/Weakable.h b/AK/Weakable.h index 84e91fc068c..605acfb2ee2 100644 --- a/AK/Weakable.h +++ b/AK/Weakable.h @@ -32,7 +32,7 @@ #include "RefPtr.h" #include "StdLibExtras.h" #ifdef KERNEL -# include +# include #endif namespace AK { diff --git a/Kernel/Arch/i386/CPU.cpp b/Kernel/Arch/i386/CPU.cpp index 9be852f6de4..a26debf3ba4 100644 --- a/Kernel/Arch/i386/CPU.cpp +++ b/Kernel/Arch/i386/CPU.cpp @@ -29,10 +29,10 @@ #include #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include #include @@ -2309,8 +2309,8 @@ UNMAP_AFTER_INIT void Processor::gdt_init() tls_descriptor.dpl = 3; tls_descriptor.segment_present = 1; tls_descriptor.granularity = 0; - tls_descriptor.zero = 0; - tls_descriptor.operation_size = 1; + tls_descriptor.operation_size64 = 0; + tls_descriptor.operation_size32 = 1; tls_descriptor.descriptor_type = 1; tls_descriptor.type = 2; write_gdt_entry(GDT_SELECTOR_TLS, tls_descriptor); // tls3 @@ -2321,8 +2321,8 @@ UNMAP_AFTER_INIT void Processor::gdt_init() fs_descriptor.dpl = 0; fs_descriptor.segment_present = 1; fs_descriptor.granularity = 0; - fs_descriptor.zero = 0; - fs_descriptor.operation_size = 1; + fs_descriptor.operation_size64 = 0; + fs_descriptor.operation_size32 = 1; fs_descriptor.descriptor_type = 1; fs_descriptor.type = 2; write_gdt_entry(GDT_SELECTOR_PROC, fs_descriptor); // fs0 @@ -2333,8 +2333,8 @@ UNMAP_AFTER_INIT void Processor::gdt_init() tss_descriptor.dpl = 0; tss_descriptor.segment_present = 1; tss_descriptor.granularity = 0; - tss_descriptor.zero = 0; - tss_descriptor.operation_size = 1; + tss_descriptor.operation_size64 = 0; + tss_descriptor.operation_size32 = 1; tss_descriptor.descriptor_type = 0; tss_descriptor.type = 9; write_gdt_entry(GDT_SELECTOR_TSS, tss_descriptor); // tss diff --git a/Kernel/Arch/i386/Interrupts.h b/Kernel/Arch/i386/InterruptEntry.cpp similarity index 67% rename from Kernel/Arch/i386/Interrupts.h rename to Kernel/Arch/i386/InterruptEntry.cpp index cfa457da160..a86267b32e0 100644 --- a/Kernel/Arch/i386/Interrupts.h +++ b/Kernel/Arch/i386/InterruptEntry.cpp @@ -24,24 +24,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#pragma once - -#include -#include -#include - -extern "C" void interrupt_common_asm_entry(); - -#if ARCH(I386) - -# define GENERATE_GENERIC_INTERRUPT_HANDLER_ASM_ENTRY(isr_number) \ - extern "C" void interrupt_##isr_number##_asm_entry(); \ - asm(".globl interrupt_" #isr_number "_asm_entry\n" \ - "interrupt_" #isr_number "_asm_entry:\n" \ - " pushw $" #isr_number "\n" \ - " pushw $0\n" \ - " jmp interrupt_common_asm_entry\n"); - +#include +#include // clang-format off asm( ".globl interrupt_common_asm_entry\n" @@ -68,9 +52,9 @@ asm( " movl %ebx, 0(%esp) \n" // push pointer to TrapFrame ".globl common_trap_exit \n" "common_trap_exit: \n" - // another thread may have handled this trap at this point, so don't - // make assumptions about the stack other than there's a TrapFrame - // and a pointer to it. + // another thread may have handled this trap at this point, so don't + // make assumptions about the stack other than there's a TrapFrame + // and a pointer to it. " call exit_trap \n" " addl $" __STRINGIFY(TRAP_FRAME_SIZE + 4) ", %esp\n" // pop TrapFrame and pointer to it ".globl interrupt_common_asm_exit \n" @@ -85,19 +69,3 @@ asm( " iret\n" ); // clang-format on - -#elif ARCH(X86_64) - -# define GENERATE_GENERIC_INTERRUPT_HANDLER_ASM_ENTRY(isr_number) \ - extern "C" void interrupt_##isr_number##_asm_entry(); \ - asm(".globl interrupt_" #isr_number "_asm_entry\n" \ - "interrupt_" #isr_number "_asm_entry:\n" \ - " cli\n" \ - " hlt\n"); - -asm( - ".globl common_trap_exit\n" - "common_trap_exit:\n" - " cli;hlt\n"); - -#endif diff --git a/Kernel/Arch/i386/ProcessorInfo.cpp b/Kernel/Arch/i386/ProcessorInfo.cpp index a7f8f4ec4d0..b5f5196a357 100644 --- a/Kernel/Arch/i386/ProcessorInfo.cpp +++ b/Kernel/Arch/i386/ProcessorInfo.cpp @@ -26,8 +26,8 @@ #include #include -#include -#include +#include +#include namespace Kernel { diff --git a/Kernel/Arch/i386/SafeMem.cpp b/Kernel/Arch/i386/SafeMem.cpp index a4be13e1d91..2e2eff662b6 100644 --- a/Kernel/Arch/i386/SafeMem.cpp +++ b/Kernel/Arch/i386/SafeMem.cpp @@ -24,8 +24,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include +#include +#include #define CODE_SECTION(section_name) __attribute__((section(section_name))) diff --git a/Kernel/Arch/i386/CPU.h b/Kernel/Arch/x86/CPU.h similarity index 94% rename from Kernel/Arch/i386/CPU.h rename to Kernel/Arch/x86/CPU.h index 54a61eb8dc6..34c5b43fa37 100644 --- a/Kernel/Arch/i386/CPU.h +++ b/Kernel/Arch/x86/CPU.h @@ -30,6 +30,10 @@ #include #include #include + +#include +#include + #include #include #include @@ -55,90 +59,6 @@ inline u32 get_iopl_from_eflags(u32 eflags) return (eflags & iopl_mask) >> 12; } -struct [[gnu::packed]] DescriptorTablePointer { - u16 limit; - void* address; -}; - -struct [[gnu::packed]] TSS32 { - u16 backlink, __blh; - u32 esp0; - u16 ss0, __ss0h; - u32 esp1; - u16 ss1, __ss1h; - u32 esp2; - u16 ss2, __ss2h; - u32 cr3, eip, eflags; - u32 eax, ecx, edx, ebx, esp, ebp, esi, edi; - u16 es, __esh; - u16 cs, __csh; - u16 ss, __ssh; - u16 ds, __dsh; - u16 fs, __fsh; - u16 gs, __gsh; - u16 ldt, __ldth; - u16 trace, iomapbase; -}; - -union [[gnu::packed]] Descriptor { - struct { - u16 limit_lo; - u16 base_lo; - u8 base_hi; - u8 type : 4; - u8 descriptor_type : 1; - u8 dpl : 2; - u8 segment_present : 1; - u8 limit_hi : 4; - u8 : 1; - u8 zero : 1; - u8 operation_size : 1; - u8 granularity : 1; - u8 base_hi2; - }; - struct { - u32 low; - u32 high; - }; - - enum Type { - Invalid = 0, - AvailableTSS_16bit = 0x1, - LDT = 0x2, - BusyTSS_16bit = 0x3, - CallGate_16bit = 0x4, - TaskGate = 0x5, - InterruptGate_16bit = 0x6, - TrapGate_16bit = 0x7, - AvailableTSS_32bit = 0x9, - BusyTSS_32bit = 0xb, - CallGate_32bit = 0xc, - InterruptGate_32bit = 0xe, - TrapGate_32bit = 0xf, - }; - - VirtualAddress base() const - { - FlatPtr base = base_lo; - base |= base_hi << 16u; - base |= base_hi2 << 24u; - return VirtualAddress { base }; - } - - void set_base(VirtualAddress base) - { - base_lo = base.get() & 0xffffu; - base_hi = (base.get() >> 16u) & 0xffu; - base_hi2 = (base.get() >> 24u) & 0xffu; - } - - void set_limit(u32 length) - { - limit_lo = length & 0xffff; - limit_hi = (length >> 16) & 0xf; - } -}; - class PageDirectoryEntry { public: const PageTableEntry* page_table_base() const { return reinterpret_cast(m_raw & 0xfffff000u); } diff --git a/Kernel/Arch/x86_64/CPU.h b/Kernel/Arch/x86/DescriptorTable.h similarity index 80% rename from Kernel/Arch/x86_64/CPU.h rename to Kernel/Arch/x86/DescriptorTable.h index e307ab606f6..990da3dbacd 100644 --- a/Kernel/Arch/x86_64/CPU.h +++ b/Kernel/Arch/x86/DescriptorTable.h @@ -30,29 +30,29 @@ #include #include +#define GDT_SELECTOR_CODE0 0x08 +#define GDT_SELECTOR_DATA0 0x10 +#define GDT_SELECTOR_CODE3 0x18 +#define GDT_SELECTOR_DATA3 0x20 +#define GDT_SELECTOR_TLS 0x28 +#define GDT_SELECTOR_PROC 0x30 +#define GDT_SELECTOR_TSS 0x38 + +// SYSENTER makes certain assumptions on how the GDT is structured: +static_assert(GDT_SELECTOR_CODE0 + 8 == GDT_SELECTOR_DATA0); // SS0 = CS0 + 8 + +// SYSEXIT makes certain assumptions on how the GDT is structured: +static_assert(GDT_SELECTOR_CODE0 + 16 == GDT_SELECTOR_CODE3); // CS3 = CS0 + 16 +static_assert(GDT_SELECTOR_CODE0 + 24 == GDT_SELECTOR_DATA3); // SS3 = CS0 + 32 + namespace Kernel { -struct [[gnu::packed]] TSS64 -{ - u32 __1; // Link? - u64 rsp0; - u64 rsp1; - u64 rsp2; - u64 __2; // IST0 -> empty? - u64 ist1; - u64 ist2; - u64 ist3; - u64 ist4; - u64 ist5; - u64 ist6; - u64 ist7; - u64 __3; - u16 __4; - u16 iomapbase; +struct [[gnu::packed]] DescriptorTablePointer { + u16 limit; + void* address; }; -union [[gnu::packed]] Descriptor -{ +union [[gnu::packed]] Descriptor { struct { u16 limit_lo; u16 base_lo; diff --git a/Kernel/Arch/i386/ISRStubs.h b/Kernel/Arch/x86/ISRStubs.h similarity index 99% rename from Kernel/Arch/i386/ISRStubs.h rename to Kernel/Arch/x86/ISRStubs.h index 86db4644a18..94714d315ef 100644 --- a/Kernel/Arch/i386/ISRStubs.h +++ b/Kernel/Arch/x86/ISRStubs.h @@ -26,7 +26,7 @@ #pragma once -#include +#include GENERATE_GENERIC_INTERRUPT_HANDLER_ASM_ENTRY(80) GENERATE_GENERIC_INTERRUPT_HANDLER_ASM_ENTRY(81) diff --git a/Kernel/Arch/x86/Interrupts.h b/Kernel/Arch/x86/Interrupts.h new file mode 100644 index 00000000000..70e53d043e1 --- /dev/null +++ b/Kernel/Arch/x86/Interrupts.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018-2021, Andreas Kling + * Copyright (c) 2021, Leon Albrecht + * 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 GenericInterruptHandeler; + +extern "C" void interrupt_common_asm_entry(); + +#define GENERATE_GENERIC_INTERRUPT_HANDLER_ASM_ENTRY(isr_number) \ + extern "C" void interrupt_##isr_number##_asm_entry(); \ + asm(".globl interrupt_" #isr_number "_asm_entry\n" \ + "interrupt_" #isr_number "_asm_entry:\n" \ + " pushw $" #isr_number "\n" \ + " pushw $0\n" \ + " jmp interrupt_common_asm_entry\n"); + +void register_interrupt_handler(u8 number, void (*handler)()); +void register_user_callable_interrupt_handler(u8 number, void (*handler)()); +GenericInterruptHandler& get_interrupt_handler(u8 interrupt_number); +void register_generic_interrupt_handler(u8 number, GenericInterruptHandler&); +void unregister_generic_interrupt_handler(u8 number, GenericInterruptHandler&); + +} diff --git a/Kernel/Arch/x86/PageDirectory.h b/Kernel/Arch/x86/PageDirectory.h new file mode 100644 index 00000000000..2a83fb6b401 --- /dev/null +++ b/Kernel/Arch/x86/PageDirectory.h @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2018-2021, 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 +#include + +namespace Kernel { + +class PageDirectory; +class PageTableEntry; + +class PageDirectoryEntry { +public: + const PageTableEntry* page_table_base() const { return reinterpret_cast(m_raw & 0xfffff000u); } + PageTableEntry* page_table_base() { return reinterpret_cast(m_raw & 0xfffff000u); } + void set_page_table_base(u32 value) + { + m_raw &= 0x8000000000000fffULL; + m_raw |= value & 0xfffff000; + } + + bool is_null() const { return m_raw == 0; } + void clear() { m_raw = 0; } + + u64 raw() const { return m_raw; } + void copy_from(Badge, const PageDirectoryEntry& other) { m_raw = other.m_raw; } + + enum Flags { + Present = 1 << 0, + ReadWrite = 1 << 1, + UserSupervisor = 1 << 2, + WriteThrough = 1 << 3, + CacheDisabled = 1 << 4, + Huge = 1 << 7, + Global = 1 << 8, + NoExecute = 0x8000000000000000ULL, + }; + + bool is_present() const { return raw() & Present; } + void set_present(bool b) { set_bit(Present, b); } + + bool is_user_allowed() const { return raw() & UserSupervisor; } + void set_user_allowed(bool b) { set_bit(UserSupervisor, b); } + + bool is_huge() const { return raw() & Huge; } + void set_huge(bool b) { set_bit(Huge, b); } + + bool is_writable() const { return raw() & ReadWrite; } + void set_writable(bool b) { set_bit(ReadWrite, b); } + + bool is_write_through() const { return raw() & WriteThrough; } + void set_write_through(bool b) { set_bit(WriteThrough, b); } + + bool is_cache_disabled() const { return raw() & CacheDisabled; } + void set_cache_disabled(bool b) { set_bit(CacheDisabled, b); } + + bool is_global() const { return raw() & Global; } + void set_global(bool b) { set_bit(Global, b); } + + bool is_execute_disabled() const { return raw() & NoExecute; } + void set_execute_disabled(bool b) { set_bit(NoExecute, b); } + + void set_bit(u64 bit, bool value) + { + if (value) + m_raw |= bit; + else + m_raw &= ~bit; + } + +private: + u64 m_raw; +}; + +class PageTableEntry { +public: + void* physical_page_base() { return reinterpret_cast(m_raw & 0xfffff000u); } + void set_physical_page_base(u32 value) + { + m_raw &= 0x8000000000000fffULL; + m_raw |= value & 0xfffff000; + } + + u64 raw() const { return (u32)m_raw; } + + enum Flags { + Present = 1 << 0, + ReadWrite = 1 << 1, + UserSupervisor = 1 << 2, + WriteThrough = 1 << 3, + CacheDisabled = 1 << 4, + Global = 1 << 8, + NoExecute = 0x8000000000000000ULL, + }; + + bool is_present() const { return raw() & Present; } + void set_present(bool b) { set_bit(Present, b); } + + bool is_user_allowed() const { return raw() & UserSupervisor; } + void set_user_allowed(bool b) { set_bit(UserSupervisor, b); } + + bool is_writable() const { return raw() & ReadWrite; } + void set_writable(bool b) { set_bit(ReadWrite, b); } + + bool is_write_through() const { return raw() & WriteThrough; } + void set_write_through(bool b) { set_bit(WriteThrough, b); } + + bool is_cache_disabled() const { return raw() & CacheDisabled; } + void set_cache_disabled(bool b) { set_bit(CacheDisabled, b); } + + bool is_global() const { return raw() & Global; } + void set_global(bool b) { set_bit(Global, b); } + + bool is_execute_disabled() const { return raw() & NoExecute; } + void set_execute_disabled(bool b) { set_bit(NoExecute, b); } + + bool is_null() const { return m_raw == 0; } + void clear() { m_raw = 0; } + + void set_bit(u64 bit, bool value) + { + if (value) + m_raw |= bit; + else + m_raw &= ~bit; + } + +private: + u64 m_raw; +}; + +static_assert(sizeof(PageDirectoryEntry) == 8); +static_assert(sizeof(PageTableEntry) == 8); + +class PageDirectoryPointerTable { +public: + PageDirectoryEntry* directory(size_t index) + { + return (PageDirectoryEntry*)(raw[index] & ~0xfffu); + } + + u64 raw[4]; +}; + +} diff --git a/Kernel/Arch/i386/ProcessorInfo.h b/Kernel/Arch/x86/ProcessorInfo.h similarity index 100% rename from Kernel/Arch/i386/ProcessorInfo.h rename to Kernel/Arch/x86/ProcessorInfo.h diff --git a/Kernel/Arch/i386/SafeMem.h b/Kernel/Arch/x86/SafeMem.h similarity index 100% rename from Kernel/Arch/i386/SafeMem.h rename to Kernel/Arch/x86/SafeMem.h diff --git a/Kernel/Arch/x86/SmapDisabler.h b/Kernel/Arch/x86/SmapDisabler.h index 3574dc733f7..3fdfd1b0ea5 100644 --- a/Kernel/Arch/x86/SmapDisabler.h +++ b/Kernel/Arch/x86/SmapDisabler.h @@ -26,7 +26,7 @@ #pragma once -#include +#include namespace Kernel { diff --git a/Kernel/Arch/x86/TSS.h b/Kernel/Arch/x86/TSS.h new file mode 100644 index 00000000000..3b0178a059a --- /dev/null +++ b/Kernel/Arch/x86/TSS.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2018-2021, Andreas Kling + * Copyright (c) 2021, Leon Albrecht + * 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 { + +struct [[gnu::packed]] TSS32 { + u16 backlink, __blh; + u32 esp0; + u16 ss0, __ss0h; + u32 esp1; + u16 ss1, __ss1h; + u32 esp2; + u16 ss2, __ss2h; + u32 cr3, eip, eflags; + u32 eax, ecx, edx, ebx, esp, ebp, esi, edi; + u16 es, __esh; + u16 cs, __csh; + u16 ss, __ssh; + u16 ds, __dsh; + u16 fs, __fsh; + u16 gs, __gsh; + u16 ldt, __ldth; + u16 trace, iomapbase; +}; + +struct [[gnu::packed]] TSS64 { + u32 __1; // Link? + u32 rsp0l; + u32 rsp0h; + u32 rsp1l; + u32 rsp1h; + u32 rsp2l; + u32 rsp2h; + u64 __2; //probably CR3 and EIP? + u32 ist1l; + u32 ist1h; + u32 ist2l; + u32 ist2h; + u32 ist3l; + u32 ist3h; + u32 ist4l; + u32 ist4h; + u32 ist5l; + u32 ist5h; + u32 ist6l; + u32 ist6h; + u32 ist7l; + u32 ist7h; + u64 __3; // GS and LDTR? + u16 __4; + u16 iomapbase; +}; + +#if ARCH(I386) +using TSS = TSS32; +#elif ARCH(X86_64) +using TSS = TSS64; +#endif + +} diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index efb30c6a234..db4a48f289c 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -20,9 +20,6 @@ set(KERNEL_SOURCES ACPI/Parser.cpp AddressSanitizer.cpp Arch/PC/BIOS.cpp - Arch/i386/CPU.cpp - Arch/i386/ProcessorInfo.cpp - Arch/i386/SafeMem.cpp Arch/x86/SmapDisabler.h CMOS.cpp CommandLine.cpp @@ -235,6 +232,14 @@ set(KERNEL_SOURCES kprintf.cpp ) +set(KERNEL_SOURCES + ${KERNEL_SOURCES} + ${CMAKE_CURRENT_SOURCE_DIR}/Arch/${KERNEL_ARCH}/CPU.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Arch/${KERNEL_ARCH}/InterruptEntry.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Arch/${KERNEL_ARCH}/ProcessorInfo.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Arch/${KERNEL_ARCH}/SafeMem.cpp +) + set(AK_SOURCES ../AK/ByteBuffer.cpp ../AK/FlyString.cpp @@ -323,6 +328,7 @@ set_source_files_properties(init.cpp PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Arch/${KERNEL_ARCH}/Boot/boot.S ) + add_library(boot OBJECT Arch/${KERNEL_ARCH}/Boot/boot.S) add_library(kernel_heap STATIC ${KERNEL_HEAP_SOURCES}) diff --git a/Kernel/Devices/FullDevice.cpp b/Kernel/Devices/FullDevice.cpp index 0dfbf416e68..4381abcaca8 100644 --- a/Kernel/Devices/FullDevice.cpp +++ b/Kernel/Devices/FullDevice.cpp @@ -27,7 +27,7 @@ #include "FullDevice.h" #include #include -#include +#include #include namespace Kernel { diff --git a/Kernel/Devices/KeyboardDevice.cpp b/Kernel/Devices/KeyboardDevice.cpp index 386d4d1f09a..428f6a9ac43 100644 --- a/Kernel/Devices/KeyboardDevice.cpp +++ b/Kernel/Devices/KeyboardDevice.cpp @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/Devices/PCSpeaker.cpp b/Kernel/Devices/PCSpeaker.cpp index db6d15902a1..a36fd4cd21c 100644 --- a/Kernel/Devices/PCSpeaker.cpp +++ b/Kernel/Devices/PCSpeaker.cpp @@ -24,7 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include #include diff --git a/Kernel/Devices/VMWareBackdoor.cpp b/Kernel/Devices/VMWareBackdoor.cpp index a295522451f..b8f220f0fdf 100644 --- a/Kernel/Devices/VMWareBackdoor.cpp +++ b/Kernel/Devices/VMWareBackdoor.cpp @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 38b055983d8..a8cdfc5aa23 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -29,8 +29,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/Kernel/Heap/kmalloc.cpp b/Kernel/Heap/kmalloc.cpp index 460148fab98..ddd2d05e566 100644 --- a/Kernel/Heap/kmalloc.cpp +++ b/Kernel/Heap/kmalloc.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/IO.h b/Kernel/IO.h index 80e11625d1a..a3331df4ae1 100644 --- a/Kernel/IO.h +++ b/Kernel/IO.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include namespace IO { diff --git a/Kernel/Interrupts/APIC.cpp b/Kernel/Interrupts/APIC.cpp index 844b2924a85..7e64f609540 100644 --- a/Kernel/Interrupts/APIC.cpp +++ b/Kernel/Interrupts/APIC.cpp @@ -30,8 +30,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/Kernel/Interrupts/GenericInterruptHandler.cpp b/Kernel/Interrupts/GenericInterruptHandler.cpp index 878e3757e34..c4519faeaca 100644 --- a/Kernel/Interrupts/GenericInterruptHandler.cpp +++ b/Kernel/Interrupts/GenericInterruptHandler.cpp @@ -24,7 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include #include diff --git a/Kernel/Interrupts/GenericInterruptHandler.h b/Kernel/Interrupts/GenericInterruptHandler.h index 73385e9276c..3ca77eb445d 100644 --- a/Kernel/Interrupts/GenericInterruptHandler.h +++ b/Kernel/Interrupts/GenericInterruptHandler.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include namespace Kernel { diff --git a/Kernel/Interrupts/IOAPIC.cpp b/Kernel/Interrupts/IOAPIC.cpp index 2435781ec9b..fc7d4827aa9 100644 --- a/Kernel/Interrupts/IOAPIC.cpp +++ b/Kernel/Interrupts/IOAPIC.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/Interrupts/IRQHandler.cpp b/Kernel/Interrupts/IRQHandler.cpp index 308eafb11db..c81177839c6 100644 --- a/Kernel/Interrupts/IRQHandler.cpp +++ b/Kernel/Interrupts/IRQHandler.cpp @@ -24,7 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include #include diff --git a/Kernel/Interrupts/IRQHandler.h b/Kernel/Interrupts/IRQHandler.h index 6034afbf096..44f7b91c8cf 100644 --- a/Kernel/Interrupts/IRQHandler.h +++ b/Kernel/Interrupts/IRQHandler.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include diff --git a/Kernel/Interrupts/InterruptManagement.cpp b/Kernel/Interrupts/InterruptManagement.cpp index d89feeef9f3..c5cc9bdf17b 100644 --- a/Kernel/Interrupts/InterruptManagement.cpp +++ b/Kernel/Interrupts/InterruptManagement.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/Interrupts/MSIHandler.h b/Kernel/Interrupts/MSIHandler.h index 278a738a24e..800110327db 100644 --- a/Kernel/Interrupts/MSIHandler.h +++ b/Kernel/Interrupts/MSIHandler.h @@ -27,7 +27,7 @@ #pragma once #include -#include +#include #include #include diff --git a/Kernel/Interrupts/PIC.cpp b/Kernel/Interrupts/PIC.cpp index 98f92cde97c..9fbda940c0a 100644 --- a/Kernel/Interrupts/PIC.cpp +++ b/Kernel/Interrupts/PIC.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include #include diff --git a/Kernel/Interrupts/SharedIRQHandler.cpp b/Kernel/Interrupts/SharedIRQHandler.cpp index 8a90049821c..a715cf030ad 100644 --- a/Kernel/Interrupts/SharedIRQHandler.cpp +++ b/Kernel/Interrupts/SharedIRQHandler.cpp @@ -24,7 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include #include diff --git a/Kernel/Interrupts/SharedIRQHandler.h b/Kernel/Interrupts/SharedIRQHandler.h index 4d1867b0be5..97d4df8fa44 100644 --- a/Kernel/Interrupts/SharedIRQHandler.h +++ b/Kernel/Interrupts/SharedIRQHandler.h @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include namespace Kernel { diff --git a/Kernel/Interrupts/SpuriousInterruptHandler.h b/Kernel/Interrupts/SpuriousInterruptHandler.h index 7c6f4f04ad6..76311494161 100644 --- a/Kernel/Interrupts/SpuriousInterruptHandler.h +++ b/Kernel/Interrupts/SpuriousInterruptHandler.h @@ -28,7 +28,7 @@ #include #include -#include +#include #include #include diff --git a/Kernel/Interrupts/UnhandledInterruptHandler.h b/Kernel/Interrupts/UnhandledInterruptHandler.h index f076aff6c65..87b0c24552e 100644 --- a/Kernel/Interrupts/UnhandledInterruptHandler.h +++ b/Kernel/Interrupts/UnhandledInterruptHandler.h @@ -28,7 +28,7 @@ #include #include -#include +#include #include namespace Kernel { diff --git a/Kernel/Lock.h b/Kernel/Lock.h index de193e18e71..137942328ab 100644 --- a/Kernel/Lock.h +++ b/Kernel/Lock.h @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/Panic.cpp b/Kernel/Panic.cpp index eccb7412f17..f133381486f 100644 --- a/Kernel/Panic.cpp +++ b/Kernel/Panic.cpp @@ -25,7 +25,7 @@ */ #include -#include +#include #include #include diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index e04bbde4613..7899f5dacb8 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/Random.cpp b/Kernel/Random.cpp index ca07e955a49..633a933469e 100644 --- a/Kernel/Random.cpp +++ b/Kernel/Random.cpp @@ -26,7 +26,7 @@ */ #include -#include +#include #include #include #include diff --git a/Kernel/Random.h b/Kernel/Random.h index 0997e4ddbf2..f267237c12d 100644 --- a/Kernel/Random.h +++ b/Kernel/Random.h @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/SpinLock.h b/Kernel/SpinLock.h index a8cd3894774..1e565a535b3 100644 --- a/Kernel/SpinLock.h +++ b/Kernel/SpinLock.h @@ -28,7 +28,7 @@ #include #include -#include +#include #include namespace Kernel { diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp index f364a0b2ff6..6fd793635ba 100644 --- a/Kernel/StdLib.cpp +++ b/Kernel/StdLib.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/Syscall.cpp b/Kernel/Syscall.cpp index a093a6f895c..a49225d3e13 100644 --- a/Kernel/Syscall.cpp +++ b/Kernel/Syscall.cpp @@ -25,7 +25,7 @@ */ #include -#include +#include #include #include #include diff --git a/Kernel/Syscalls/sysconf.cpp b/Kernel/Syscalls/sysconf.cpp index 21eb7252478..d67ab229b6a 100644 --- a/Kernel/Syscalls/sysconf.cpp +++ b/Kernel/Syscalls/sysconf.cpp @@ -24,7 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include #include diff --git a/Kernel/TTY/VirtualConsole.cpp b/Kernel/TTY/VirtualConsole.cpp index 9ccddbed958..252d0bc6f59 100644 --- a/Kernel/TTY/VirtualConsole.cpp +++ b/Kernel/TTY/VirtualConsole.cpp @@ -27,7 +27,7 @@ #include "VirtualConsole.h" #include -#include +#include #include #include #include diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 84252c03353..414f4d8d2f2 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/Thread.h b/Kernel/Thread.h index 67c83ca7d98..20cfbf83b2d 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -37,8 +37,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/Kernel/ThreadTracer.cpp b/Kernel/ThreadTracer.cpp index 2f426aba1f5..969734a4ce4 100644 --- a/Kernel/ThreadTracer.cpp +++ b/Kernel/ThreadTracer.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include #include namespace Kernel { diff --git a/Kernel/Time/APICTimer.cpp b/Kernel/Time/APICTimer.cpp index 109b648a419..245f2d40d4a 100644 --- a/Kernel/Time/APICTimer.cpp +++ b/Kernel/Time/APICTimer.cpp @@ -24,7 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include #include diff --git a/Kernel/Time/PIT.cpp b/Kernel/Time/PIT.cpp index 43e45dfc503..6b6b133bc8f 100644 --- a/Kernel/Time/PIT.cpp +++ b/Kernel/Time/PIT.cpp @@ -24,7 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include #include diff --git a/Kernel/Time/RTC.cpp b/Kernel/Time/RTC.cpp index 38e39f8b88b..cbe9960f058 100644 --- a/Kernel/Time/RTC.cpp +++ b/Kernel/Time/RTC.cpp @@ -24,7 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include #include diff --git a/Kernel/VM/MemoryManager.cpp b/Kernel/VM/MemoryManager.cpp index d5935ee7de6..c264c81a8ce 100644 --- a/Kernel/VM/MemoryManager.cpp +++ b/Kernel/VM/MemoryManager.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/VM/MemoryManager.h b/Kernel/VM/MemoryManager.h index 15ba26888f8..5e5ff9b48b4 100644 --- a/Kernel/VM/MemoryManager.h +++ b/Kernel/VM/MemoryManager.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/VM/PhysicalPage.h b/Kernel/VM/PhysicalPage.h index 19d23060575..850ef75bf84 100644 --- a/Kernel/VM/PhysicalPage.h +++ b/Kernel/VM/PhysicalPage.h @@ -27,7 +27,7 @@ #pragma once #include -#include +#include #include #include #include diff --git a/Kernel/VM/Range.cpp b/Kernel/VM/Range.cpp index f1dd42c5924..fb2ca402223 100644 --- a/Kernel/VM/Range.cpp +++ b/Kernel/VM/Range.cpp @@ -26,7 +26,7 @@ */ #include -#include +#include #include namespace Kernel { diff --git a/Kernel/VM/Region.h b/Kernel/VM/Region.h index 43676b09c51..eec65704533 100644 --- a/Kernel/VM/Region.h +++ b/Kernel/VM/Region.h @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/init.cpp b/Kernel/init.cpp index a5d24a3c060..71615fe84d8 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include