diff --git a/kernel/arch/i386/APIC.cpp b/kernel/arch/i386/APIC.cpp index 0ae5b45e..746d1f6a 100644 --- a/kernel/arch/i386/APIC.cpp +++ b/kernel/arch/i386/APIC.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -210,6 +211,7 @@ namespace APIC static void ParseMADT(RSDPDescriptor* rsdp) { RSDT* root = (RSDT*)(rsdp->revision == 2 ? ((RSDPDescriptor20*)rsdp)->xsdt_address : rsdp->rsdt_address); + Paging::MapRSDP((uint32_t)root & 0xFFC00000); uint32_t sdt_entry_count = (root->header.length - sizeof(root->header)) / (rsdp->revision == 2 ? 8 : 4); for (uint32_t i = 0; i < sdt_entry_count; i++) @@ -370,6 +372,11 @@ namespace APIC if (s_local_apic == 0 || s_io_apic == 0) return false; + if ((s_io_apic & 0xFFC00000) != (s_local_apic & 0xFFC00000)) + Kernel::panic("lapic and ioapic are not in the same 4 MiB are"); + + Paging::MapAPIC(s_io_apic & 0xFFC00000); + // Enable Local APIC SetMSR(IA32_APIC_BASE, (s_local_apic & 0xFFFFF000) | IA32_APIC_BASE_ENABLE, 0); diff --git a/kernel/arch/i386/Paging.cpp b/kernel/arch/i386/Paging.cpp new file mode 100644 index 00000000..bc8428a8 --- /dev/null +++ b/kernel/arch/i386/Paging.cpp @@ -0,0 +1,73 @@ +#include +#include + +namespace Paging +{ + + static uint32_t s_page_directory[1024] __attribute__((aligned(4096))); + static uint32_t s_page_table[1024] __attribute__((aligned(4096))); + static uint32_t s_page_table_framebuffer[1024] __attribute__((aligned(4096))); + static uint32_t s_page_table_rsdp[1024] __attribute__((aligned(4096))); + static uint32_t s_page_table_apic[1024] __attribute__((aligned(4096))); + + void Initialize() + { + for (uint32_t i = 0; i < 1024; i++) + s_page_directory[i] = 0x00000002; + + for (uint32_t i = 0; i < 1024; i++) + s_page_table[i] = (i << 12) | 0x03; + s_page_directory[0] = (uint32_t)s_page_table | 0x03; + + asm volatile( + "movl %%eax, %%cr3;" + "movl %%cr0, %%eax;" + "orl $0x80000001, %%eax;" + "movl %%eax, %%cr0;" + :: "a"(s_page_directory) + ); + } + + void MapFramebuffer(uint32_t address) + { + uint32_t pd_index = address >> 22; + + if (!(s_page_directory[pd_index] & (1 << 0))) + { + for (uint32_t i = 0; i < 1024; i++) + s_page_table_framebuffer[i] = address | (i << 12) | 0x03; + s_page_directory[pd_index] = (uint32_t)s_page_table_framebuffer | 0x03; + for (uint32_t i = 0; i < 1024; i++) + asm volatile("invlpg (%0)" :: "r" (address | (i << 12)) : "memory"); + } + } + + void MapRSDP(uint32_t address) + { + uint32_t pd_index = address >> 22; + + if (!(s_page_directory[pd_index] & (1 << 0))) + { + for (uint32_t i = 0; i < 1024; i++) + s_page_table_rsdp[i] = address | (i << 12) | 0x03; + s_page_directory[pd_index] = (uint32_t)s_page_table_rsdp | 0x03; + for (uint32_t i = 0; i < 1024; i++) + asm volatile("invlpg (%0)" :: "r" (address | (i << 12)) : "memory"); + } + } + + void MapAPIC(uint32_t address) + { + uint32_t pd_index = address >> 22; + + if (!(s_page_directory[pd_index] & (1 << 0))) + { + for (uint32_t i = 0; i < 1024; i++) + s_page_table_apic[i] = address | (i << 12) | 0x03; + s_page_directory[pd_index] = (uint32_t)s_page_table_apic | 0x03; + for (uint32_t i = 0; i < 1024; i++) + asm volatile("invlpg (%0)" :: "r" (address | (i << 12)) : "memory"); + } + } + +} \ No newline at end of file diff --git a/kernel/arch/i386/VESA.cpp b/kernel/arch/i386/VESA.cpp index 4cc1a196..2f5832a8 100644 --- a/kernel/arch/i386/VESA.cpp +++ b/kernel/arch/i386/VESA.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -82,6 +83,8 @@ namespace VESA s_height = framebuffer.height; s_mode = framebuffer.type; + Paging::MapFramebuffer(framebuffer.addr & 0xFFC00000); + if (s_mode == MULTIBOOT_FRAMEBUFFER_TYPE_GRAPHICS) { if (s_bpp != 24 && s_bpp != 32) diff --git a/kernel/arch/i386/make.config b/kernel/arch/i386/make.config index 10de656d..602250af 100644 --- a/kernel/arch/i386/make.config +++ b/kernel/arch/i386/make.config @@ -10,5 +10,6 @@ $(ARCHDIR)/CPUID.o \ $(ARCHDIR)/font.o \ $(ARCHDIR)/GDT.o \ $(ARCHDIR)/IDT.o \ +$(ARCHDIR)/Paging.o \ $(ARCHDIR)/TTY.o \ $(ARCHDIR)/VESA.o \ diff --git a/kernel/include/kernel/Paging.h b/kernel/include/kernel/Paging.h new file mode 100644 index 00000000..eb51d1ff --- /dev/null +++ b/kernel/include/kernel/Paging.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +namespace Paging +{ + + void Initialize(); + + void MapFramebuffer(uint32_t address); + void MapRSDP(uint32_t address); + void MapAPIC(uint32_t address); + +} \ No newline at end of file diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index 58eb12e0..aba5cde9 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -50,6 +51,8 @@ extern "C" void kernel_main(multiboot_info_t* mbi, uint32_t magic) return; } + Paging::Initialize(); + s_multiboot_info = mbi; if (!VESA::Initialize())