feat: add paging
This commit is contained in:
@@ -19,6 +19,7 @@ Checks: >
|
|||||||
-readability-named-parameter,
|
-readability-named-parameter,
|
||||||
-readability-convert-member-functions-to-static,
|
-readability-convert-member-functions-to-static,
|
||||||
-performance-enum-size,
|
-performance-enum-size,
|
||||||
|
-performance-no-int-to-ptr,
|
||||||
-cppcoreguidelines-avoid-magic-numbers,
|
-cppcoreguidelines-avoid-magic-numbers,
|
||||||
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
|
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
|
||||||
-cppcoreguidelines-pro-type-union-access,
|
-cppcoreguidelines-pro-type-union-access,
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ CXXFLAGS := \
|
|||||||
-fno-rtti \
|
-fno-rtti \
|
||||||
-fno-stack-protector \
|
-fno-stack-protector \
|
||||||
-fno-stack-check \
|
-fno-stack-check \
|
||||||
|
-fno-strict-aliasing \
|
||||||
-fno-threadsafe-statics \
|
-fno-threadsafe-statics \
|
||||||
-fno-pic \
|
-fno-pic \
|
||||||
-fno-pie \
|
-fno-pie \
|
||||||
-fno-use-cxa-atexit \
|
-fno-use-cxa-atexit \
|
||||||
-mno-red-zone \
|
-mno-red-zone \
|
||||||
|
-mcmodel=kernel \
|
||||||
-Wall \
|
-Wall \
|
||||||
-Wextra \
|
-Wextra \
|
||||||
-Wpedantic \
|
-Wpedantic \
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "./idt.hpp"
|
#include "./idt.hpp"
|
||||||
#include "./task.hpp"
|
#include "./task.hpp"
|
||||||
#include "arch/arch.hpp"
|
#include "arch/arch.hpp"
|
||||||
|
#include "arch/x86_64/memory.hpp"
|
||||||
|
|
||||||
namespace x86_64 {
|
namespace x86_64 {
|
||||||
|
|
||||||
@@ -13,10 +14,19 @@ public:
|
|||||||
static arch& get();
|
static arch& get();
|
||||||
public:
|
public:
|
||||||
void init() override;
|
void init() override;
|
||||||
|
public:
|
||||||
|
pmm& get_pmm() { return m_pmm; }
|
||||||
|
paging& get_paging() { return m_paging; }
|
||||||
|
// vm& get_vm() { return m_vm; }
|
||||||
private:
|
private:
|
||||||
tss m_tss{};
|
tss m_tss{};
|
||||||
gdt m_gdt{ m_tss };
|
gdt m_gdt{ m_tss };
|
||||||
idt m_idt;
|
idt m_idt;
|
||||||
|
|
||||||
|
// Memory
|
||||||
|
pmm m_pmm;
|
||||||
|
paging m_paging{ m_pmm };
|
||||||
|
// vm m_vm;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace x86_64
|
} // namespace x86_64
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ multiboot_header:
|
|||||||
.long 8
|
.long 8
|
||||||
multiboot_header_end:
|
multiboot_header_end:
|
||||||
|
|
||||||
.section .text
|
.section .text.boot
|
||||||
.code32
|
.code32
|
||||||
|
|
||||||
.global _start
|
.global _start
|
||||||
@@ -27,15 +27,23 @@ _start:
|
|||||||
movl %eax, multiboot_magic
|
movl %eax, multiboot_magic
|
||||||
movl %ebx, multiboot_info
|
movl %ebx, multiboot_info
|
||||||
|
|
||||||
movl $stack_top, %esp
|
movl $__boot_stack_top, %esp
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bootstrap page tables
|
||||||
|
*
|
||||||
|
* PML4[0] = PML4[511] = PDPT
|
||||||
|
* PDPT[0] = PDPT[510] = PD
|
||||||
|
*/
|
||||||
movl $pdpt_table, %eax
|
movl $pdpt_table, %eax
|
||||||
orl $0x3, %eax
|
orl $0x3, %eax
|
||||||
movl %eax, pml4_table
|
movl %eax, pml4_table
|
||||||
|
movl %eax, pml4_table + (511 * 8)
|
||||||
|
|
||||||
movl $pd_table, %eax
|
movl $pd_table, %eax
|
||||||
orl $0x3, %eax
|
orl $0x3, %eax
|
||||||
movl %eax, pdpt_table
|
movl %eax, pdpt_table
|
||||||
|
movl %eax, pdpt_table + (510 * 8)
|
||||||
|
|
||||||
xorl %ecx, %ecx
|
xorl %ecx, %ecx
|
||||||
movl $pd_table, %edi
|
movl $pd_table, %edi
|
||||||
@@ -65,7 +73,7 @@ _start:
|
|||||||
movl $0xC0000080, %ecx
|
movl $0xC0000080, %ecx
|
||||||
rdmsr
|
rdmsr
|
||||||
|
|
||||||
orl $0x100, %eax
|
orl $0x900, %eax
|
||||||
|
|
||||||
wrmsr
|
wrmsr
|
||||||
|
|
||||||
@@ -100,7 +108,7 @@ long_mode_entry:
|
|||||||
movw %ax, %fs
|
movw %ax, %fs
|
||||||
movw %ax, %gs
|
movw %ax, %gs
|
||||||
|
|
||||||
movq $stack_top, %rsp
|
movq $__boot_stack_top, %rsp
|
||||||
|
|
||||||
andq $-16, %rsp
|
andq $-16, %rsp
|
||||||
|
|
||||||
@@ -110,7 +118,8 @@ long_mode_entry:
|
|||||||
|
|
||||||
call enable_sse
|
call enable_sse
|
||||||
|
|
||||||
call kmain
|
movabs $kmain, %rax
|
||||||
|
jmp *%rax
|
||||||
.hang:
|
.hang:
|
||||||
cli
|
cli
|
||||||
hlt
|
hlt
|
||||||
@@ -154,7 +163,7 @@ gdt_descriptor:
|
|||||||
.long gdt
|
.long gdt
|
||||||
|
|
||||||
|
|
||||||
.section .bss
|
.section .bss.boot
|
||||||
|
|
||||||
.align 8
|
.align 8
|
||||||
|
|
||||||
@@ -182,9 +191,12 @@ pdpt_table:
|
|||||||
pd_table:
|
pd_table:
|
||||||
.skip 4096
|
.skip 4096
|
||||||
|
|
||||||
|
.section .stack.boot, "aw", @nobits
|
||||||
.align 16
|
.align 16
|
||||||
|
|
||||||
stack_bottom:
|
.global __boot_stack_bottom
|
||||||
|
__boot_stack_bottom:
|
||||||
.skip 16384
|
.skip 16384
|
||||||
stack_top:
|
|
||||||
|
.global __boot_stack_top
|
||||||
|
__boot_stack_top:
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ isr_err_stub 30
|
|||||||
isr_nerr_stub 31
|
isr_nerr_stub 31
|
||||||
|
|
||||||
.macro isr_label i
|
.macro isr_label i
|
||||||
.long isr_stub_\i
|
.quad isr_stub_\i
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
.global isrStubTable
|
.global isrStubTable
|
||||||
|
|||||||
@@ -0,0 +1,125 @@
|
|||||||
|
#ifndef ARCH_X86_64_MEMORY_HPP
|
||||||
|
#define ARCH_X86_64_MEMORY_HPP
|
||||||
|
|
||||||
|
#include "mmap.hpp"
|
||||||
|
#include "types.hpp"
|
||||||
|
|
||||||
|
namespace x86_64 {
|
||||||
|
|
||||||
|
using virtaddr_t = u64;
|
||||||
|
using physaddr_t = u64;
|
||||||
|
|
||||||
|
static constexpr physaddr_t INVALID_PHYSADDR = 0xFFFFFFFFFFFFFFFFULL;
|
||||||
|
|
||||||
|
// Physical Memory Manager
|
||||||
|
struct pmm {
|
||||||
|
pmm();
|
||||||
|
|
||||||
|
physaddr_t alloc_page();
|
||||||
|
void free_page(physaddr_t addr);
|
||||||
|
|
||||||
|
physaddr_t alloc_page_bootstrap();
|
||||||
|
|
||||||
|
const mmap& get_mmap() const { return m_mmap; }
|
||||||
|
|
||||||
|
void relocate_bitmap(u64 hhdmBase) { m_bitmap = reinterpret_cast<u64*>(m_bitmapPaddr + hhdmBase); }
|
||||||
|
private:
|
||||||
|
static constexpr u64 PAGE_SIZE = 0x1000;
|
||||||
|
private:
|
||||||
|
void parse_multiboot();
|
||||||
|
private:
|
||||||
|
void mark_used(physaddr_t addr);
|
||||||
|
void mark_free(physaddr_t addr);
|
||||||
|
|
||||||
|
void mark_range_used(physaddr_t addr, u64 length);
|
||||||
|
void mark_range_free(physaddr_t addr, u64 length);
|
||||||
|
|
||||||
|
bool is_free(u64 frame);
|
||||||
|
void set_used(u64 frame);
|
||||||
|
void set_free(u64 frame);
|
||||||
|
|
||||||
|
void init_bitmap(u64 maxPhys);
|
||||||
|
|
||||||
|
physaddr_t find_bitmap_location(u64 bytes);
|
||||||
|
private:
|
||||||
|
mmap m_mmap{};
|
||||||
|
|
||||||
|
physaddr_t m_bitmapPaddr = INVALID_PHYSADDR;
|
||||||
|
u64* m_bitmap = nullptr;
|
||||||
|
|
||||||
|
u64 m_frameCount = 0;
|
||||||
|
u64 m_bitmapWords = 0;
|
||||||
|
u64 m_bootstrapHint = 1;
|
||||||
|
u64 m_hint = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct paging {
|
||||||
|
friend void map_range(paging& paging, const char begin[], const char end[], u64 flags);
|
||||||
|
|
||||||
|
enum page_flags {
|
||||||
|
PAGE_PRESENT = (1 << 0),
|
||||||
|
PAGE_WRITABLE = (1 << 1),
|
||||||
|
PAGE_USER = (1 << 2),
|
||||||
|
PAGE_WRITETHROUGH = (1 << 3),
|
||||||
|
PAGE_CACHEDISABLED = (1 << 4),
|
||||||
|
PAGE_ACCESSED = (1 << 5),
|
||||||
|
PAGE_DIRTY = (1 << 6),
|
||||||
|
PAGE_LARGE = (1 << 7),
|
||||||
|
PAGE_GLOBAL = (1 << 8),
|
||||||
|
|
||||||
|
PAGE_XD = (1ULL << 63),
|
||||||
|
|
||||||
|
PAGE_FLAGS_MASK = PAGE_PRESENT | PAGE_WRITABLE | PAGE_USER | PAGE_WRITETHROUGH | PAGE_CACHEDISABLED |
|
||||||
|
PAGE_ACCESSED | PAGE_DIRTY | PAGE_LARGE | PAGE_GLOBAL | PAGE_XD,
|
||||||
|
};
|
||||||
|
public:
|
||||||
|
paging(pmm& pmm);
|
||||||
|
physaddr_t get(virtaddr_t virtualAddr);
|
||||||
|
void map(physaddr_t physicalAddr, virtaddr_t virtualAddr, u64 flags);
|
||||||
|
physaddr_t unmap(virtaddr_t virtualAddr);
|
||||||
|
private:
|
||||||
|
static constexpr physaddr_t PHYS_MASK = 0x000FFFFFFFFFF000ULL;
|
||||||
|
static constexpr virtaddr_t ENTRY_INDEX_MASK = 0x1FFULL;
|
||||||
|
|
||||||
|
static constexpr virtaddr_t HHDM_BASE = 0xFFFF800000000000ULL;
|
||||||
|
private:
|
||||||
|
static physaddr_t virt_to_phys(virtaddr_t addr) { return addr & ~HHDM_BASE; }
|
||||||
|
static void* phys_to_virt(physaddr_t addr) { return reinterpret_cast<void*>(HHDM_BASE + addr); }
|
||||||
|
private:
|
||||||
|
void map2m(physaddr_t physicalAddr, virtaddr_t virtualAddr, u64 flags);
|
||||||
|
|
||||||
|
void map_hhdm(physaddr_t begin, physaddr_t end);
|
||||||
|
|
||||||
|
void allocate_table(u64& entry);
|
||||||
|
|
||||||
|
void flush_tlb(u64 addr);
|
||||||
|
private:
|
||||||
|
pmm* m_pmm;
|
||||||
|
physaddr_t m_pml4Phys;
|
||||||
|
u64* m_pml4;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Virtual Memory
|
||||||
|
/*struct vm {
|
||||||
|
vm();
|
||||||
|
|
||||||
|
virtaddr_t reserve(u64 size);
|
||||||
|
bool commit(virtaddr_t addr, u64 size);
|
||||||
|
bool decommit(virtaddr_t addr, u64 size);
|
||||||
|
void release(virtaddr_t addr, u64 size);
|
||||||
|
private:
|
||||||
|
struct range {
|
||||||
|
range* prev;
|
||||||
|
range* next;
|
||||||
|
|
||||||
|
u64 begin;
|
||||||
|
u64 size;
|
||||||
|
u64 committedPages;
|
||||||
|
};
|
||||||
|
private:
|
||||||
|
range* m_freeRanges;
|
||||||
|
};*/
|
||||||
|
|
||||||
|
} // namespace x86_64
|
||||||
|
|
||||||
|
#endif // ARCH_X86_64_MEMORY_HPP
|
||||||
@@ -0,0 +1,252 @@
|
|||||||
|
#include "./memory.hpp"
|
||||||
|
#include "mmap.hpp"
|
||||||
|
#include "vga.hpp"
|
||||||
|
|
||||||
|
namespace x86_64 {
|
||||||
|
|
||||||
|
extern "C" char __boot_stack_start[]; // NOLINT
|
||||||
|
extern "C" char __boot_stack_end[]; // NOLINT
|
||||||
|
extern "C" char __kernel_text_start[]; // NOLINT
|
||||||
|
extern "C" char __kernel_text_end[]; // NOLINT
|
||||||
|
extern "C" char __kernel_rodata_start[]; // NOLINT
|
||||||
|
extern "C" char __kernel_rodata_end[]; // NOLINT
|
||||||
|
extern "C" char __kernel_data_start[]; // NOLINT
|
||||||
|
extern "C" char __kernel_data_end[]; // NOLINT
|
||||||
|
extern "C" char __kernel_bss_start[]; // NOLINT
|
||||||
|
extern "C" char __kernel_bss_end[]; // NOLINT
|
||||||
|
|
||||||
|
extern "C" char __kernel_phys_text_start[]; // NOLINT
|
||||||
|
extern "C" char __kernel_phys_rodata_start[]; // NOLINT
|
||||||
|
extern "C" char __kernel_phys_data_start[]; // NOLINT
|
||||||
|
extern "C" char __kernel_phys_bss_start[]; // NOLINT
|
||||||
|
|
||||||
|
static constexpr u64 PAGE_2M = 0x200000ULL;
|
||||||
|
static constexpr u64 BOOTSTRAP_HHDM_LIMIT = 0x40000000ULL;
|
||||||
|
|
||||||
|
void map_range(paging& paging, const char* virtualBegin, const char* virtualEnd, physaddr_t physicalBegin, u64 flags) {
|
||||||
|
static constexpr u64 PAGE_MASK = ~0xFFFULL;
|
||||||
|
|
||||||
|
virtaddr_t virtualAddr = reinterpret_cast<virtaddr_t>(virtualBegin) & PAGE_MASK;
|
||||||
|
|
||||||
|
const virtaddr_t endAddr = (reinterpret_cast<virtaddr_t>(virtualEnd) + 0xFFF) & PAGE_MASK;
|
||||||
|
|
||||||
|
physicalBegin &= PAGE_MASK;
|
||||||
|
|
||||||
|
for (; virtualAddr < endAddr; virtualAddr += 0x1000, physicalBegin += 0x1000)
|
||||||
|
paging.map(physicalBegin, virtualAddr, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
void panic() {
|
||||||
|
vga::puts("I panicked nya~", 0, 0);
|
||||||
|
asm volatile("cli; hlt");
|
||||||
|
}
|
||||||
|
|
||||||
|
paging::paging(pmm& pmm)
|
||||||
|
: m_pmm(&pmm), m_pml4Phys(pmm.alloc_page_bootstrap()), m_pml4(reinterpret_cast<u64*>(m_pml4Phys)) {
|
||||||
|
for (u64 i = 0; i < 512; ++i)
|
||||||
|
m_pml4[i] = 0;
|
||||||
|
|
||||||
|
for (auto *entry = pmm.get_mmap().entries, *end = entry + pmm.get_mmap().entryCount; entry != end; ++entry) {
|
||||||
|
if (entry->type != mmap::entry::AVAILABLE) continue;
|
||||||
|
const physaddr_t begin = entry->base;
|
||||||
|
const physaddr_t entryEnd = entry->base + entry->size;
|
||||||
|
if (begin >= BOOTSTRAP_HHDM_LIMIT) continue;
|
||||||
|
|
||||||
|
map_hhdm(begin, entryEnd < BOOTSTRAP_HHDM_LIMIT ? entryEnd : BOOTSTRAP_HHDM_LIMIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (physaddr_t physical = reinterpret_cast<physaddr_t>(__boot_stack_start),
|
||||||
|
end = reinterpret_cast<physaddr_t>(__boot_stack_end);
|
||||||
|
physical < end;
|
||||||
|
physical += 0x1000) {
|
||||||
|
map(physical, physical, PAGE_WRITABLE | PAGE_XD);
|
||||||
|
}
|
||||||
|
|
||||||
|
map_range(*this,
|
||||||
|
__kernel_rodata_start,
|
||||||
|
__kernel_rodata_end,
|
||||||
|
reinterpret_cast<physaddr_t>(__kernel_phys_rodata_start),
|
||||||
|
PAGE_GLOBAL | PAGE_XD);
|
||||||
|
|
||||||
|
map_range(*this,
|
||||||
|
__kernel_data_start,
|
||||||
|
__kernel_data_end,
|
||||||
|
reinterpret_cast<physaddr_t>(__kernel_phys_data_start),
|
||||||
|
PAGE_GLOBAL | PAGE_WRITABLE | PAGE_XD);
|
||||||
|
|
||||||
|
map_range(*this,
|
||||||
|
__kernel_bss_start,
|
||||||
|
__kernel_bss_end,
|
||||||
|
reinterpret_cast<physaddr_t>(__kernel_phys_bss_start),
|
||||||
|
PAGE_GLOBAL | PAGE_WRITABLE | PAGE_XD);
|
||||||
|
|
||||||
|
map(0xB8000, 0xB8000, PAGE_WRITABLE | PAGE_XD);
|
||||||
|
|
||||||
|
map(m_pml4Phys, m_pml4Phys, PAGE_WRITABLE);
|
||||||
|
|
||||||
|
map_range(*this,
|
||||||
|
__kernel_text_start,
|
||||||
|
__kernel_text_end,
|
||||||
|
reinterpret_cast<physaddr_t>(__kernel_phys_text_start),
|
||||||
|
PAGE_GLOBAL);
|
||||||
|
|
||||||
|
asm volatile("mov %0, %%cr3" : : "r"(m_pml4Phys) : "memory");
|
||||||
|
|
||||||
|
m_pml4 = reinterpret_cast<u64*>(m_pml4Phys + HHDM_BASE);
|
||||||
|
pmm.relocate_bitmap(HHDM_BASE);
|
||||||
|
|
||||||
|
// TODO: Map the rest of RAM
|
||||||
|
}
|
||||||
|
|
||||||
|
physaddr_t paging::get(virtaddr_t virtualAddr) {
|
||||||
|
u64 addrInt = reinterpret_cast<u64>(virtualAddr);
|
||||||
|
|
||||||
|
u64 pml4Idx = (addrInt >> 39) & ENTRY_INDEX_MASK;
|
||||||
|
u64 pdptIdx = (addrInt >> 30) & ENTRY_INDEX_MASK;
|
||||||
|
u64 pdIdx = (addrInt >> 21) & ENTRY_INDEX_MASK;
|
||||||
|
u64 ptIdx = (addrInt >> 12) & ENTRY_INDEX_MASK;
|
||||||
|
|
||||||
|
u64 pml4e = m_pml4[pml4Idx];
|
||||||
|
if ((pml4e & 0x01) == 0) return INVALID_PHYSADDR;
|
||||||
|
u64 pdpte = reinterpret_cast<u64*>(pml4e & PHYS_MASK)[pdptIdx];
|
||||||
|
if ((pdpte & 0x01) == 0) return INVALID_PHYSADDR;
|
||||||
|
u64 pde = reinterpret_cast<u64*>(pdpte & PHYS_MASK)[pdIdx];
|
||||||
|
if ((pde & 0x01) == 0) return INVALID_PHYSADDR;
|
||||||
|
u64 pte = reinterpret_cast<u64*>(pde & PHYS_MASK)[ptIdx];
|
||||||
|
if ((pte & 0x01) == 0) return INVALID_PHYSADDR;
|
||||||
|
|
||||||
|
return (pte & PHYS_MASK) | (addrInt & ((1 << 12) - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void paging::map(physaddr_t physicalAddr, virtaddr_t virtualAddr, u64 flags) {
|
||||||
|
const u64 addrInt = reinterpret_cast<u64>(virtualAddr);
|
||||||
|
|
||||||
|
const u64 pml4Idx = (addrInt >> 39) & ENTRY_INDEX_MASK;
|
||||||
|
const u64 pdptIdx = (addrInt >> 30) & ENTRY_INDEX_MASK;
|
||||||
|
const u64 pdIdx = (addrInt >> 21) & ENTRY_INDEX_MASK;
|
||||||
|
const u64 ptIdx = (addrInt >> 12) & ENTRY_INDEX_MASK;
|
||||||
|
|
||||||
|
const bool bootstrap = reinterpret_cast<virtaddr_t>(m_pml4) < HHDM_BASE;
|
||||||
|
|
||||||
|
auto tableFromPhys = [bootstrap](physaddr_t physical) -> u64* {
|
||||||
|
return bootstrap ? reinterpret_cast<u64*>(physical) : reinterpret_cast<u64*>(HHDM_BASE + physical);
|
||||||
|
};
|
||||||
|
|
||||||
|
u64& pml4e = m_pml4[pml4Idx];
|
||||||
|
if ((pml4e & PAGE_PRESENT) == 0) allocate_table(pml4e);
|
||||||
|
|
||||||
|
u64* pdpt = tableFromPhys(pml4e & PHYS_MASK);
|
||||||
|
u64& pdpte = pdpt[pdptIdx];
|
||||||
|
if ((pdpte & PAGE_PRESENT) == 0) allocate_table(pdpte);
|
||||||
|
|
||||||
|
u64* pd = tableFromPhys(pdpte & PHYS_MASK);
|
||||||
|
u64& pde = pd[pdIdx];
|
||||||
|
if ((pde & PAGE_PRESENT) == 0) allocate_table(pde);
|
||||||
|
if ((pde & PAGE_LARGE) == PAGE_LARGE) panic();
|
||||||
|
|
||||||
|
u64* pt = tableFromPhys(pde & PHYS_MASK);
|
||||||
|
u64& pte = pt[ptIdx];
|
||||||
|
|
||||||
|
const bool flushNeeded = (pte & PAGE_PRESENT) == PAGE_PRESENT;
|
||||||
|
|
||||||
|
pte = (physicalAddr & PHYS_MASK) | (flags & PAGE_FLAGS_MASK) | PAGE_PRESENT;
|
||||||
|
|
||||||
|
if (flushNeeded) flush_tlb(addrInt & ~0xFFFULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
physaddr_t paging::unmap(virtaddr_t virtualAddr) {
|
||||||
|
u64 addrInt = reinterpret_cast<u64>(virtualAddr);
|
||||||
|
|
||||||
|
u64 pml4Idx = (addrInt >> 39) & ENTRY_INDEX_MASK;
|
||||||
|
u64 pdptIdx = (addrInt >> 30) & ENTRY_INDEX_MASK;
|
||||||
|
u64 pdIdx = (addrInt >> 21) & ENTRY_INDEX_MASK;
|
||||||
|
u64 ptIdx = (addrInt >> 12) & ENTRY_INDEX_MASK;
|
||||||
|
|
||||||
|
u64& pml4e = m_pml4[pml4Idx];
|
||||||
|
if ((pml4e & 0x01) == 0) return INVALID_PHYSADDR;
|
||||||
|
u64& pdpte = reinterpret_cast<u64*>(phys_to_virt(pml4e & PHYS_MASK))[pdptIdx];
|
||||||
|
if ((pdpte & 0x01) == 0) return INVALID_PHYSADDR;
|
||||||
|
u64& pde = reinterpret_cast<u64*>(phys_to_virt(pdpte & PHYS_MASK))[pdIdx];
|
||||||
|
if ((pde & 0x01) == 0) return INVALID_PHYSADDR;
|
||||||
|
u64& pte = reinterpret_cast<u64*>(phys_to_virt(pde & PHYS_MASK))[ptIdx];
|
||||||
|
if ((pte & 0x01) == 0) return INVALID_PHYSADDR;
|
||||||
|
physaddr_t oldAddr = pte & PHYS_MASK;
|
||||||
|
pte = 0;
|
||||||
|
|
||||||
|
flush_tlb(addrInt & ~0xFFFULL);
|
||||||
|
return oldAddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void paging::map2m(physaddr_t physicalAddr, virtaddr_t virtualAddr, u64 flags) {
|
||||||
|
const u64 addrInt = reinterpret_cast<u64>(virtualAddr);
|
||||||
|
|
||||||
|
if ((physicalAddr & (PAGE_2M - 1)) != 0 || (addrInt & (PAGE_2M - 1)) != 0) {
|
||||||
|
panic();
|
||||||
|
}
|
||||||
|
|
||||||
|
const u64 pml4Idx = (addrInt >> 39) & ENTRY_INDEX_MASK;
|
||||||
|
const u64 pdptIdx = (addrInt >> 30) & ENTRY_INDEX_MASK;
|
||||||
|
const u64 pdIdx = (addrInt >> 21) & ENTRY_INDEX_MASK;
|
||||||
|
|
||||||
|
const bool bootstrap = reinterpret_cast<virtaddr_t>(m_pml4) < HHDM_BASE;
|
||||||
|
|
||||||
|
auto tableFromPhys = [bootstrap](physaddr_t physical) -> u64* {
|
||||||
|
return bootstrap ? reinterpret_cast<u64*>(physical) : reinterpret_cast<u64*>(HHDM_BASE + physical);
|
||||||
|
};
|
||||||
|
|
||||||
|
u64& pml4e = m_pml4[pml4Idx];
|
||||||
|
if ((pml4e & PAGE_PRESENT) == 0) allocate_table(pml4e);
|
||||||
|
|
||||||
|
u64* pdpt = tableFromPhys(pml4e & PHYS_MASK);
|
||||||
|
u64& pdpte = pdpt[pdptIdx];
|
||||||
|
if ((pdpte & PAGE_PRESENT) == 0) allocate_table(pdpte);
|
||||||
|
|
||||||
|
u64* pd = tableFromPhys(pdpte & PHYS_MASK);
|
||||||
|
u64& pde = pd[pdIdx];
|
||||||
|
if ((pde & PAGE_PRESENT) == PAGE_PRESENT) panic();
|
||||||
|
|
||||||
|
pde = (physicalAddr & PHYS_MASK) | (flags & PAGE_FLAGS_MASK) | PAGE_PRESENT | PAGE_LARGE;
|
||||||
|
|
||||||
|
flush_tlb(addrInt & ~0xFFFULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void paging::map_hhdm(physaddr_t begin, physaddr_t end) {
|
||||||
|
if (begin >= end) return;
|
||||||
|
|
||||||
|
begin &= ~0xFFFULL;
|
||||||
|
end = (end + 0xFFFULL) & ~0xFFFULL;
|
||||||
|
|
||||||
|
while (begin < end && (begin & (PAGE_2M - 1)) != 0) {
|
||||||
|
map(begin, HHDM_BASE + begin, PAGE_WRITABLE | PAGE_XD);
|
||||||
|
|
||||||
|
begin += 0x1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (begin + PAGE_2M <= end) {
|
||||||
|
map2m(begin, HHDM_BASE + begin, PAGE_WRITABLE | PAGE_XD);
|
||||||
|
|
||||||
|
begin += PAGE_2M;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; begin < end; begin += 0x1000)
|
||||||
|
map(begin, HHDM_BASE + begin, PAGE_WRITABLE | PAGE_XD);
|
||||||
|
}
|
||||||
|
|
||||||
|
void paging::allocate_table(u64& entry) {
|
||||||
|
const bool bootstrap = (reinterpret_cast<virtaddr_t>(m_pml4) < HHDM_BASE);
|
||||||
|
|
||||||
|
physaddr_t paddr = bootstrap ? m_pmm->alloc_page_bootstrap() : m_pmm->alloc_page();
|
||||||
|
if (paddr == INVALID_PHYSADDR) panic();
|
||||||
|
|
||||||
|
auto* table = reinterpret_cast<u64*>(bootstrap ? reinterpret_cast<void*>(paddr) : phys_to_virt(paddr));
|
||||||
|
for (u64 i = 0; i < 512; ++i)
|
||||||
|
table[i] = 0;
|
||||||
|
|
||||||
|
entry = (paddr & PHYS_MASK) | PAGE_WRITABLE | PAGE_PRESENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
void paging::flush_tlb(u64 addr) {
|
||||||
|
asm volatile("invlpg (%0)" ::"r"(addr) : "memory");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace x86_64
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
#include "./memory.hpp"
|
||||||
|
#include "mmap.hpp"
|
||||||
|
#include "multiboot2.hpp"
|
||||||
|
|
||||||
|
namespace x86_64 {
|
||||||
|
|
||||||
|
extern "C" char __kernel_phys_start[]; // NOLINT
|
||||||
|
extern "C" char __kernel_phys_end[]; // NOLINT
|
||||||
|
|
||||||
|
static const physaddr_t KERNEL_START_PADDR = reinterpret_cast<physaddr_t>(__kernel_phys_start);
|
||||||
|
static const physaddr_t KERNEL_END_PADDR = reinterpret_cast<physaddr_t>(__kernel_phys_end);
|
||||||
|
|
||||||
|
extern "C" multiboot_info_header* multiboot_info; // NOLINT
|
||||||
|
|
||||||
|
pmm::pmm() {
|
||||||
|
physaddr_t maxPhys = 0;
|
||||||
|
|
||||||
|
parse_multiboot();
|
||||||
|
for (u64 i = 0; i < m_mmap.entryCount; ++i) {
|
||||||
|
const auto& entry = m_mmap.entries[i];
|
||||||
|
if (entry.type != mmap::entry::AVAILABLE) continue;
|
||||||
|
if (maxPhys < entry.base + entry.size) maxPhys = entry.base + entry.size; // NOLINT
|
||||||
|
}
|
||||||
|
|
||||||
|
init_bitmap(maxPhys);
|
||||||
|
}
|
||||||
|
|
||||||
|
physaddr_t pmm::alloc_page() {
|
||||||
|
for (u64 frame = m_hint; frame < m_frameCount; ++frame) {
|
||||||
|
if (!is_free(frame)) continue;
|
||||||
|
|
||||||
|
set_used(frame);
|
||||||
|
m_hint = frame + 1;
|
||||||
|
|
||||||
|
return frame * PAGE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return INVALID_PHYSADDR;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm::free_page(physaddr_t addr) {
|
||||||
|
if ((addr & (PAGE_SIZE - 1)) != 0) return;
|
||||||
|
|
||||||
|
const u64 frame = addr / PAGE_SIZE;
|
||||||
|
|
||||||
|
if (frame >= m_frameCount) return;
|
||||||
|
|
||||||
|
set_free(frame);
|
||||||
|
|
||||||
|
if (frame < m_hint) m_hint = frame; // NOLINT
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr u64 BOOTSTRAP_LIMIT = 0x40000000ULL;
|
||||||
|
|
||||||
|
physaddr_t pmm::alloc_page_bootstrap() {
|
||||||
|
constexpr u64 MAX_FRAME = BOOTSTRAP_LIMIT / PAGE_SIZE;
|
||||||
|
|
||||||
|
for (u64 frame = m_bootstrapHint; frame < MAX_FRAME; ++frame) {
|
||||||
|
if (!is_free(frame)) continue;
|
||||||
|
|
||||||
|
set_used(frame);
|
||||||
|
m_bootstrapHint = frame + 1;
|
||||||
|
|
||||||
|
return frame * PAGE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return INVALID_PHYSADDR;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm::parse_multiboot() {
|
||||||
|
for (auto* tag = reinterpret_cast<multiboot_tag*>(multiboot_info + 1);;
|
||||||
|
tag = reinterpret_cast<multiboot_tag*>((reinterpret_cast<u64>(tag) + tag->size + 7) & ~0x7ULL)) {
|
||||||
|
if (tag->size < sizeof(multiboot_tag)) break;
|
||||||
|
if (tag->type == multiboot_tag::NONE) break;
|
||||||
|
|
||||||
|
switch (tag->type) {
|
||||||
|
case multiboot_tag::MMAP: {
|
||||||
|
auto* mmap = reinterpret_cast<multiboot_tag_mmap*>(tag);
|
||||||
|
if (mmap->entrySize < sizeof(multiboot_mmap_entry)) break;
|
||||||
|
for (u8 *ptr = reinterpret_cast<u8*>(mmap + 1), *end = reinterpret_cast<u8*>(tag) + tag->size; ptr != end;
|
||||||
|
ptr += mmap->entrySize) {
|
||||||
|
multiboot_mmap_entry entry = *reinterpret_cast<multiboot_mmap_entry*>(ptr);
|
||||||
|
m_mmap.entries[m_mmap.entryCount++] = { entry.addr, entry.len, mmap::entry::type_e(entry.type) };
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm::mark_used(physaddr_t addr) {
|
||||||
|
mark_range_used(addr, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm::mark_free(physaddr_t addr) {
|
||||||
|
mark_range_free(addr, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm::mark_range_used(physaddr_t addr, u64 length) {
|
||||||
|
const u64 begin = (addr & ~0xFFF) / PAGE_SIZE;
|
||||||
|
const u64 end = ((addr + length + PAGE_SIZE - 1) & ~0xFFF) / PAGE_SIZE;
|
||||||
|
|
||||||
|
for (u64 frame = begin; frame < end; ++frame)
|
||||||
|
set_used(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm::mark_range_free(physaddr_t addr, u64 length) {
|
||||||
|
const u64 begin = (addr & ~0xFFF) / PAGE_SIZE;
|
||||||
|
const u64 end = ((addr + length + PAGE_SIZE - 1) & ~0xFFF) / PAGE_SIZE;
|
||||||
|
|
||||||
|
for (u64 frame = begin; frame < end; ++frame)
|
||||||
|
set_free(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool pmm::is_free(u64 frame) {
|
||||||
|
return (m_bitmap[frame / 64] & (1ULL << (frame & 63))) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm::set_used(u64 frame) {
|
||||||
|
m_bitmap[frame / 64] |= 1ULL << (frame & 63);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm::set_free(u64 frame) {
|
||||||
|
m_bitmap[frame / 64] &= ~(1ULL << (frame & 63));
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm::init_bitmap(u64 maxPhys) {
|
||||||
|
m_frameCount = (maxPhys + PAGE_SIZE - 1) / PAGE_SIZE;
|
||||||
|
|
||||||
|
const u64 bitmapBytes = (m_frameCount + 7) / 8;
|
||||||
|
|
||||||
|
static constexpr u64 BITS_PER_WORD = sizeof(u64) * 8;
|
||||||
|
|
||||||
|
const u64 bitmapWords = (m_frameCount + BITS_PER_WORD - 1) / BITS_PER_WORD;
|
||||||
|
|
||||||
|
m_bitmapPaddr = find_bitmap_location(bitmapBytes);
|
||||||
|
m_bitmap = reinterpret_cast<u64*>(m_bitmapPaddr);
|
||||||
|
|
||||||
|
for (u64 i = 0; i < bitmapWords; ++i)
|
||||||
|
m_bitmap[i] = ~0ULL;
|
||||||
|
|
||||||
|
for (u64 i = 0; i < m_mmap.entryCount; ++i) {
|
||||||
|
const auto& entry = m_mmap.entries[i];
|
||||||
|
if (entry.type != mmap::entry::AVAILABLE) continue;
|
||||||
|
mark_range_free(entry.base, entry.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
mark_range_used(KERNEL_START_PADDR, KERNEL_END_PADDR - KERNEL_START_PADDR);
|
||||||
|
|
||||||
|
mark_range_used(m_bitmapPaddr, bitmapBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
physaddr_t pmm::find_bitmap_location(u64 bytes) {
|
||||||
|
const u64 size = (bytes + PAGE_SIZE - 1) & ~0xFFFULL;
|
||||||
|
|
||||||
|
for (u64 i = 0; i < m_mmap.entryCount; ++i) {
|
||||||
|
const auto& entry = m_mmap.entries[i];
|
||||||
|
if (entry.type != mmap::entry::AVAILABLE) continue;
|
||||||
|
|
||||||
|
u64 begin = (entry.base + PAGE_SIZE - 1) & ~0xFFFULL;
|
||||||
|
u64 end = entry.base + entry.size;
|
||||||
|
if (begin >= BOOTSTRAP_LIMIT) continue;
|
||||||
|
|
||||||
|
if (BOOTSTRAP_LIMIT < end) end = BOOTSTRAP_LIMIT; // NOLINT
|
||||||
|
if (end - begin < size) continue;
|
||||||
|
return (end - size + PAGE_SIZE - 1) & ~0xFFFULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return INVALID_PHYSADDR;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace x86_64
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
#include "arch/arch.hpp"
|
#include "arch/arch.hpp"
|
||||||
#include "vga.hpp"
|
#include "vga.hpp"
|
||||||
|
|
||||||
extern "C" void kmain(void) {
|
extern "C" [[noreturn]] void kmain(void) {
|
||||||
vga::clear();
|
vga::clear();
|
||||||
|
|
||||||
arch::get().init();
|
arch::get().init();
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef MMAP_HPP
|
||||||
|
#define MMAP_HPP
|
||||||
|
|
||||||
|
#include "types.hpp"
|
||||||
|
|
||||||
|
struct mmap {
|
||||||
|
static constexpr u64 MAX_ENTRY_COUNT = 170;
|
||||||
|
|
||||||
|
struct entry {
|
||||||
|
enum type_e {
|
||||||
|
INVALID = 0,
|
||||||
|
AVAILABLE = 1,
|
||||||
|
RESERVED = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
u64 base = 0;
|
||||||
|
u64 size = 0;
|
||||||
|
type_e type = AVAILABLE;
|
||||||
|
};
|
||||||
|
|
||||||
|
u64 entryCount = 0;
|
||||||
|
entry entries[MAX_ENTRY_COUNT];
|
||||||
|
};
|
||||||
|
|
||||||
|
static_assert(sizeof(mmap) <= 4096);
|
||||||
|
|
||||||
|
#endif // MMAP_HPP
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef MULTIBOOT2_HPP
|
||||||
|
#define MULTIBOOT2_HPP
|
||||||
|
|
||||||
|
#include "types.hpp"
|
||||||
|
|
||||||
|
struct multiboot_info_header {
|
||||||
|
u32 totalSize;
|
||||||
|
u32 reserved;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
struct multiboot_tag {
|
||||||
|
static constexpr u32 NONE = 0;
|
||||||
|
static constexpr u32 MMAP = 6;
|
||||||
|
|
||||||
|
u32 type;
|
||||||
|
u32 size;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
struct multiboot_mmap_entry {
|
||||||
|
u64 addr;
|
||||||
|
u64 len;
|
||||||
|
u32 type;
|
||||||
|
u32 zero;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
struct multiboot_tag_mmap : multiboot_tag {
|
||||||
|
u32 entrySize;
|
||||||
|
u32 entryVersion;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
#endif // MULTIBOOT2_HPP
|
||||||
@@ -1,30 +1,82 @@
|
|||||||
ENTRY(_start)
|
ENTRY(_start)
|
||||||
|
|
||||||
|
BOOT_LMA = 1M;
|
||||||
|
KERNEL_LMA = 2M;
|
||||||
|
|
||||||
|
KERNEL_VMA = 0xffffffff80000000 + KERNEL_LMA;
|
||||||
|
|
||||||
|
PHDRS
|
||||||
|
{
|
||||||
|
boot PT_LOAD FLAGS(5);
|
||||||
|
kernel_text PT_LOAD FLAGS(5);
|
||||||
|
kernel_ro PT_LOAD FLAGS(4);
|
||||||
|
kernel_data PT_LOAD FLAGS(6);
|
||||||
|
}
|
||||||
|
|
||||||
SECTIONS
|
SECTIONS
|
||||||
{
|
{
|
||||||
. = 1M;
|
. = BOOT_LMA;
|
||||||
|
|
||||||
.multiboot ALIGN(8) :
|
.multiboot ALIGN(8) :
|
||||||
{
|
{
|
||||||
KEEP(*(.multiboot))
|
KEEP(*(.multiboot))
|
||||||
}
|
} :boot
|
||||||
|
|
||||||
.text ALIGN(16) :
|
.text.boot ALIGN(16) :
|
||||||
{
|
{
|
||||||
|
KEEP(*(.text.boot))
|
||||||
|
KEEP(*(.text.boot.*))
|
||||||
|
} :boot
|
||||||
|
|
||||||
|
.bss.boot ALIGN(16) (NOLOAD) :
|
||||||
|
{
|
||||||
|
KEEP(*(.bss.boot))
|
||||||
|
KEEP(*(.bss.boot.*))
|
||||||
|
} :boot
|
||||||
|
|
||||||
|
.stack.boot ALIGN(16) (NOLOAD) :
|
||||||
|
{
|
||||||
|
__boot_stack_start = .;
|
||||||
|
|
||||||
|
KEEP(*(.stack.boot));
|
||||||
|
|
||||||
|
__boot_stack_end = .;
|
||||||
|
} :boot
|
||||||
|
|
||||||
|
. = KERNEL_VMA;
|
||||||
|
|
||||||
|
.text ALIGN(16) : AT(KERNEL_LMA)
|
||||||
|
{
|
||||||
|
__kernel_text_start = .;
|
||||||
|
|
||||||
*(.text .text.*)
|
*(.text .text.*)
|
||||||
}
|
|
||||||
|
|
||||||
.rodata ALIGN(16) :
|
__kernel_text_end = .;
|
||||||
|
} :kernel_text
|
||||||
|
|
||||||
|
__kernel_phys_text_start = LOADADDR(.text);
|
||||||
|
|
||||||
|
.rodata ALIGN(16) : AT(KERNEL_LMA + (ADDR(.rodata) - KERNEL_VMA))
|
||||||
{
|
{
|
||||||
|
__kernel_rodata_start = .;
|
||||||
|
|
||||||
*(.rodata .rodata.*)
|
*(.rodata .rodata.*)
|
||||||
}
|
|
||||||
|
|
||||||
.data ALIGN(16) :
|
__kernel_rodata_end = .;
|
||||||
|
} :kernel_ro
|
||||||
|
|
||||||
|
__kernel_phys_rodata_start = LOADADDR(.rodata);
|
||||||
|
|
||||||
|
.data ALIGN(16) : AT(KERNEL_LMA + (ADDR(.data) - KERNEL_VMA))
|
||||||
{
|
{
|
||||||
*(.data .data.*)
|
__kernel_data_start = .;
|
||||||
}
|
|
||||||
|
|
||||||
.init_array ALIGN(8) :
|
*(.data .data.*)
|
||||||
|
} :kernel_data
|
||||||
|
|
||||||
|
__kernel_phys_data_start = LOADADDR(.data);
|
||||||
|
|
||||||
|
.init_array ALIGN(8) : AT(KERNEL_LMA + (ADDR(.init_array) - KERNEL_VMA))
|
||||||
{
|
{
|
||||||
__init_array_start = .;
|
__init_array_start = .;
|
||||||
|
|
||||||
@@ -32,13 +84,26 @@ SECTIONS
|
|||||||
KEEP(*(.init_array))
|
KEEP(*(.init_array))
|
||||||
|
|
||||||
__init_array_end = .;
|
__init_array_end = .;
|
||||||
}
|
__kernel_data_end = .;
|
||||||
|
} :kernel_data
|
||||||
|
|
||||||
.bss ALIGN(16) :
|
.bss ALIGN(16) (NOLOAD) : AT(KERNEL_LMA + (ADDR(.bss) - KERNEL_VMA))
|
||||||
{
|
{
|
||||||
|
__kernel_bss_start = .;
|
||||||
|
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
*(.bss .bss.*)
|
*(.bss .bss.*)
|
||||||
}
|
|
||||||
|
__kernel_bss_end = .;
|
||||||
|
} :kernel_data
|
||||||
|
|
||||||
|
__kernel_phys_bss_start = LOADADDR(.bss);
|
||||||
|
|
||||||
|
__kernel_start = ADDR(.stack.boot);
|
||||||
|
__kernel_end = .;
|
||||||
|
|
||||||
|
__kernel_phys_start = LOADADDR(.text);
|
||||||
|
__kernel_phys_end = LOADADDR(.bss) + SIZEOF(.bss);
|
||||||
|
|
||||||
/DISCARD/ :
|
/DISCARD/ :
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user