Kernel: Enable basic paging

This commit is contained in:
Bananymous 2022-12-28 04:17:06 +02:00
parent 3e8590687f
commit 5d7a767f8b
6 changed files with 101 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <kernel/IDT.h>
#include <kernel/IO.h>
#include <kernel/kprint.h>
#include <kernel/Paging.h>
#include <kernel/panic.h>
#include <kernel/PIC.h>
#include <kernel/Serial.h>
@ -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);

View File

@ -0,0 +1,73 @@
#include <kernel/Paging.h>
#include <kernel/panic.h>
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");
}
}
}

View File

@ -1,6 +1,7 @@
#include <kernel/IO.h>
#include <kernel/kmalloc.h>
#include <kernel/multiboot.h>
#include <kernel/Paging.h>
#include <kernel/panic.h>
#include <kernel/Serial.h>
#include <kernel/VESA.h>
@ -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)

View File

@ -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 \

View File

@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
namespace Paging
{
void Initialize();
void MapFramebuffer(uint32_t address);
void MapRSDP(uint32_t address);
void MapAPIC(uint32_t address);
}

View File

@ -8,6 +8,7 @@
#include <kernel/kmalloc.h>
#include <kernel/kprint.h>
#include <kernel/multiboot.h>
#include <kernel/Paging.h>
#include <kernel/panic.h>
#include <kernel/PIC.h>
#include <kernel/PIT.h>
@ -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())