Usuń kernel bo jest niepotrzebny
pls
This commit is contained in:
@@ -1,18 +0,0 @@
|
|||||||
#ifndef ARCH_HPP
|
|
||||||
#define ARCH_HPP
|
|
||||||
|
|
||||||
struct arch {
|
|
||||||
arch() = default;
|
|
||||||
virtual ~arch() = default;
|
|
||||||
|
|
||||||
arch(arch&&) noexcept = default;
|
|
||||||
arch& operator=(arch&&) noexcept = default;
|
|
||||||
arch(const arch&) = default;
|
|
||||||
arch& operator=(const arch&) = default;
|
|
||||||
|
|
||||||
virtual void init() = 0;
|
|
||||||
|
|
||||||
static arch& get();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // ARCH_HPP
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#include "./arch.hpp"
|
|
||||||
|
|
||||||
#include "arch/x86_64/int.hpp"
|
|
||||||
#include "runtime.hpp" // IWYU pragma: keep
|
|
||||||
|
|
||||||
arch& arch::get() {
|
|
||||||
return x86_64::arch::get();
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
arch::arch() {}
|
|
||||||
|
|
||||||
arch& arch::get() {
|
|
||||||
static x86_64::arch s_arch;
|
|
||||||
return s_arch;
|
|
||||||
}
|
|
||||||
|
|
||||||
void arch::init() {
|
|
||||||
m_gdt.load();
|
|
||||||
reload_segments(gdt::KERNEL_CS, gdt::KERNEL_DS);
|
|
||||||
|
|
||||||
m_idt.load();
|
|
||||||
|
|
||||||
m_picController.remap(0x20, 0x28);
|
|
||||||
|
|
||||||
static constexpr hwirq_t PIT_HWIRQ = 0x00;
|
|
||||||
|
|
||||||
irq_t pitIrq = m_irqManager.allocate();
|
|
||||||
m_irqManager.bind(pitIrq, m_picController, PIT_HWIRQ, 0x20);
|
|
||||||
m_irqManager.register_handler(pitIrq, pit::handler, &m_pit);
|
|
||||||
m_irqManager.enable(pitIrq);
|
|
||||||
|
|
||||||
asm volatile("sti");
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
#ifndef ARCH_X86_64_ARCH_HPP
|
|
||||||
#define ARCH_X86_64_ARCH_HPP
|
|
||||||
|
|
||||||
#include "./gdt.hpp"
|
|
||||||
#include "./idt.hpp"
|
|
||||||
#include "./task.hpp"
|
|
||||||
#include "arch/arch.hpp"
|
|
||||||
#include "arch/x86_64/int.hpp"
|
|
||||||
#include "arch/x86_64/memory.hpp"
|
|
||||||
#include "arch/x86_64/pic.hpp"
|
|
||||||
#include "arch/x86_64/pit.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
class arch : public ::arch {
|
|
||||||
private:
|
|
||||||
arch();
|
|
||||||
public:
|
|
||||||
static arch& get();
|
|
||||||
public:
|
|
||||||
void init() override;
|
|
||||||
public:
|
|
||||||
pmm& get_pmm() { return m_pmm; }
|
|
||||||
paging& get_paging() { return m_paging; }
|
|
||||||
vm& get_vm() { return m_vm; }
|
|
||||||
public:
|
|
||||||
irq_manager& get_irq_manager() { return m_irqManager; }
|
|
||||||
|
|
||||||
pic& get_pic() { return m_picController; }
|
|
||||||
private:
|
|
||||||
tss m_tss{};
|
|
||||||
gdt m_gdt{ m_tss };
|
|
||||||
idt m_idt;
|
|
||||||
|
|
||||||
// Memory
|
|
||||||
pmm m_pmm;
|
|
||||||
paging m_paging{ m_pmm };
|
|
||||||
vm m_vm{ m_pmm };
|
|
||||||
|
|
||||||
// Interrupts
|
|
||||||
pic m_picController;
|
|
||||||
|
|
||||||
pit m_pit;
|
|
||||||
|
|
||||||
irq_manager m_irqManager{ m_vm };
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
|
|
||||||
#endif // ARCH_X86_64_ARCH_HPP
|
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
.section .multiboot
|
|
||||||
.align 8
|
|
||||||
|
|
||||||
multiboot_header:
|
|
||||||
.long 0xE85250D6
|
|
||||||
.long 0
|
|
||||||
.long multiboot_header_end - multiboot_header
|
|
||||||
.long -(0xE85250D6 + 0 + (multiboot_header_end - multiboot_header))
|
|
||||||
|
|
||||||
.short 0
|
|
||||||
.short 0
|
|
||||||
.long 8
|
|
||||||
multiboot_header_end:
|
|
||||||
|
|
||||||
.section .text.boot
|
|
||||||
.code32
|
|
||||||
|
|
||||||
.global _start
|
|
||||||
.type _start, @function
|
|
||||||
|
|
||||||
.extern kmain
|
|
||||||
|
|
||||||
_start:
|
|
||||||
cli
|
|
||||||
cld
|
|
||||||
|
|
||||||
movl %eax, multiboot_magic
|
|
||||||
movl %ebx, multiboot_info
|
|
||||||
|
|
||||||
movl $__boot_stack_top, %esp
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bootstrap page tables
|
|
||||||
*
|
|
||||||
* PML4[0] = PML4[511] = PDPT
|
|
||||||
* PDPT[0] = PDPT[510] = PD
|
|
||||||
*/
|
|
||||||
movl $pdpt_table, %eax
|
|
||||||
orl $0x3, %eax
|
|
||||||
movl %eax, pml4_table
|
|
||||||
movl %eax, pml4_table + (511 * 8)
|
|
||||||
|
|
||||||
movl $pd_table, %eax
|
|
||||||
orl $0x3, %eax
|
|
||||||
movl %eax, pdpt_table
|
|
||||||
movl %eax, pdpt_table + (510 * 8)
|
|
||||||
|
|
||||||
xorl %ecx, %ecx
|
|
||||||
movl $pd_table, %edi
|
|
||||||
|
|
||||||
.fill_pd:
|
|
||||||
movl %ecx, %eax
|
|
||||||
|
|
||||||
shll $21, %eax
|
|
||||||
|
|
||||||
orl $0x83, %eax
|
|
||||||
|
|
||||||
movl %eax, (%edi)
|
|
||||||
|
|
||||||
addl $8, %edi
|
|
||||||
incl %ecx
|
|
||||||
|
|
||||||
cmpl $512, %ecx
|
|
||||||
jne .fill_pd
|
|
||||||
|
|
||||||
movl $pml4_table, %eax
|
|
||||||
movl %eax, %cr3
|
|
||||||
|
|
||||||
movl %cr4, %eax
|
|
||||||
orl $0x20, %eax
|
|
||||||
movl %eax, %cr4
|
|
||||||
|
|
||||||
movl $0xC0000080, %ecx
|
|
||||||
rdmsr
|
|
||||||
|
|
||||||
orl $0x900, %eax
|
|
||||||
|
|
||||||
wrmsr
|
|
||||||
|
|
||||||
movl %cr0, %eax
|
|
||||||
orl $0x80000001, %eax
|
|
||||||
movl %eax, %cr0
|
|
||||||
|
|
||||||
lgdt gdt_descriptor
|
|
||||||
|
|
||||||
ljmp $0x08, $long_mode_entry
|
|
||||||
|
|
||||||
|
|
||||||
.code64
|
|
||||||
|
|
||||||
enable_sse:
|
|
||||||
movq %cr0, %rax
|
|
||||||
andw $0xFFFB, %ax
|
|
||||||
orw $0x0002, %ax
|
|
||||||
movq %rax, %cr0
|
|
||||||
|
|
||||||
movq %cr4, %rax
|
|
||||||
orw $0x600, %ax
|
|
||||||
movq %rax, %cr4
|
|
||||||
ret
|
|
||||||
|
|
||||||
long_mode_entry:
|
|
||||||
movw $0x10, %ax
|
|
||||||
|
|
||||||
movw %ax, %ds
|
|
||||||
movw %ax, %es
|
|
||||||
movw %ax, %ss
|
|
||||||
movw %ax, %fs
|
|
||||||
movw %ax, %gs
|
|
||||||
|
|
||||||
movq $__boot_stack_top, %rsp
|
|
||||||
|
|
||||||
andq $-16, %rsp
|
|
||||||
|
|
||||||
xorq %rbp, %rbp
|
|
||||||
|
|
||||||
call run_global_constructors
|
|
||||||
|
|
||||||
call enable_sse
|
|
||||||
|
|
||||||
movabs $kmain, %rax
|
|
||||||
jmp *%rax
|
|
||||||
.hang:
|
|
||||||
cli
|
|
||||||
hlt
|
|
||||||
jmp .hang
|
|
||||||
|
|
||||||
|
|
||||||
.extern __init_array_start
|
|
||||||
.extern __init_array_end
|
|
||||||
|
|
||||||
run_global_constructors:
|
|
||||||
pushq %rbp
|
|
||||||
movq %rsp, %rbp
|
|
||||||
|
|
||||||
movq $__init_array_start, %rax
|
|
||||||
|
|
||||||
.constructor_loop:
|
|
||||||
cmpq $__init_array_end, %rax
|
|
||||||
jae .constructors_done
|
|
||||||
|
|
||||||
movq (%rax), %rdx
|
|
||||||
call *%rdx
|
|
||||||
|
|
||||||
addq $8, %rax
|
|
||||||
jmp .constructor_loop
|
|
||||||
|
|
||||||
.constructors_done:
|
|
||||||
popq %rbp
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
.align 8
|
|
||||||
|
|
||||||
gdt:
|
|
||||||
.quad 0x0000000000000000
|
|
||||||
.quad 0x00AF9A000000FFFF
|
|
||||||
.quad 0x00AF92000000FFFF
|
|
||||||
gdt_end:
|
|
||||||
|
|
||||||
gdt_descriptor:
|
|
||||||
.word gdt_end - gdt - 1
|
|
||||||
.long gdt
|
|
||||||
|
|
||||||
|
|
||||||
.section .bss.boot
|
|
||||||
|
|
||||||
.align 8
|
|
||||||
|
|
||||||
.global multiboot_magic
|
|
||||||
multiboot_magic:
|
|
||||||
.quad 0
|
|
||||||
|
|
||||||
.global multiboot_info
|
|
||||||
multiboot_info:
|
|
||||||
.quad 0
|
|
||||||
|
|
||||||
|
|
||||||
.align 4096
|
|
||||||
|
|
||||||
pml4_table:
|
|
||||||
.skip 4096
|
|
||||||
|
|
||||||
.align 4096
|
|
||||||
|
|
||||||
pdpt_table:
|
|
||||||
.skip 4096
|
|
||||||
|
|
||||||
.align 4096
|
|
||||||
|
|
||||||
pd_table:
|
|
||||||
.skip 4096
|
|
||||||
|
|
||||||
.section .stack.boot, "aw", @nobits
|
|
||||||
.align 16
|
|
||||||
|
|
||||||
.global __boot_stack_bottom
|
|
||||||
__boot_stack_bottom:
|
|
||||||
.skip 16384
|
|
||||||
|
|
||||||
.global __boot_stack_top
|
|
||||||
__boot_stack_top:
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
.section .text
|
|
||||||
.code64
|
|
||||||
|
|
||||||
.global reload_segments
|
|
||||||
.type reload_segments, @function
|
|
||||||
|
|
||||||
reload_segments:
|
|
||||||
pushq %rdi
|
|
||||||
leaq .reload_cs(%rip), %rax
|
|
||||||
pushq %rax
|
|
||||||
lretq
|
|
||||||
.reload_cs:
|
|
||||||
movw %si, %ax
|
|
||||||
movw %ax, %ds
|
|
||||||
movw %ax, %es
|
|
||||||
movw %ax, %fs
|
|
||||||
movw %ax, %gs
|
|
||||||
movw %ax, %ss
|
|
||||||
ret
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#include "./gdt.hpp"
|
|
||||||
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
struct gdt_descriptor {
|
|
||||||
u16 size;
|
|
||||||
const void* offset;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
void gdt::load() {
|
|
||||||
gdt_descriptor gdtr{ (sizeof(entries) - 1), entries };
|
|
||||||
|
|
||||||
asm volatile("lgdt %0" : : "m"(gdtr));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
#ifndef ARCH_X86_64_GDT_HPP
|
|
||||||
#define ARCH_X86_64_GDT_HPP
|
|
||||||
|
|
||||||
#include "arch/x86_64/task.hpp"
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
struct gdt_entry {
|
|
||||||
u64 value;
|
|
||||||
|
|
||||||
constexpr gdt_entry(u32 base, u32 limit, u8 access, u8 flags) noexcept
|
|
||||||
: value((static_cast<u64>(limit & 0xFFFF) << 0) | (static_cast<u64>(base & 0xFFFFFF) << 16) |
|
|
||||||
(static_cast<u64>(access & 0xFF) << 40) | (static_cast<u64>(limit & 0x0F) << 48) |
|
|
||||||
(static_cast<u64>(flags & 0x0F) << 52) | (static_cast<u64>((base >> 24) & 0xFF) << 56)) {}
|
|
||||||
|
|
||||||
constexpr gdt_entry(u64 value = 0) noexcept
|
|
||||||
: value(value) {}
|
|
||||||
|
|
||||||
static gdt_entry extend(u32 baseHi) { return { baseHi }; }
|
|
||||||
|
|
||||||
enum {
|
|
||||||
ACCESS_P = 1 << 7, /**< Present */
|
|
||||||
ACCESS_DPL0 = 0, /**< Highest privilege */
|
|
||||||
ACCESS_DPL3 = 3 << 5, /**< Lowest privilege */
|
|
||||||
ACCESS_S = 1 << 4, /**< Code/data segment */
|
|
||||||
ACCESS_E = 1 << 3, /**< Executable */
|
|
||||||
ACCESS_DC = 1 << 2, /**< Direction/Conforming */
|
|
||||||
ACCESS_RW = 1 << 1, /**< Readable/Writeable */
|
|
||||||
ACCESS_A = 1 << 0, /**< Accessed */
|
|
||||||
|
|
||||||
TYPE_LDT = 0x2,
|
|
||||||
TYPE_AVAIL = 0x9,
|
|
||||||
TYPE_BUSY = 0xB,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum {
|
|
||||||
FLAG_G = 1 << 3, /**< Granularity */
|
|
||||||
FLAG_DB = 1 << 2, /**< Size */
|
|
||||||
FLAG_L = 1 << 1, /**< Long mode */
|
|
||||||
};
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
struct gdt {
|
|
||||||
gdt_entry entries[7] = {
|
|
||||||
// Null Descriptor
|
|
||||||
gdt_entry{ 0, 0, 0, 0 },
|
|
||||||
// Kernel Mode Code Segment
|
|
||||||
gdt_entry{ 0,
|
|
||||||
0xFFFFF,
|
|
||||||
gdt_entry::ACCESS_P | gdt_entry::ACCESS_DPL0 | gdt_entry::ACCESS_S | gdt_entry::ACCESS_E |
|
|
||||||
gdt_entry::ACCESS_RW,
|
|
||||||
gdt_entry::FLAG_G | gdt_entry::FLAG_L },
|
|
||||||
// Kernel Mode Data Segment
|
|
||||||
gdt_entry{ 0,
|
|
||||||
0xFFFFF,
|
|
||||||
gdt_entry::ACCESS_P | gdt_entry::ACCESS_DPL0 | gdt_entry::ACCESS_S | gdt_entry::ACCESS_RW,
|
|
||||||
gdt_entry::FLAG_G | gdt_entry::FLAG_DB },
|
|
||||||
// User Mode Data Segment
|
|
||||||
gdt_entry{ 0,
|
|
||||||
0xFFFFF,
|
|
||||||
gdt_entry::ACCESS_P | gdt_entry::ACCESS_DPL3 | gdt_entry::ACCESS_S | gdt_entry::ACCESS_RW,
|
|
||||||
gdt_entry::FLAG_G | gdt_entry::FLAG_DB },
|
|
||||||
// User Mode Code Segment
|
|
||||||
gdt_entry{ 0,
|
|
||||||
0xFFFFF,
|
|
||||||
gdt_entry::ACCESS_P | gdt_entry::ACCESS_DPL3 | gdt_entry::ACCESS_S | gdt_entry::ACCESS_E |
|
|
||||||
gdt_entry::ACCESS_RW,
|
|
||||||
gdt_entry::FLAG_G | gdt_entry::FLAG_L },
|
|
||||||
};
|
|
||||||
|
|
||||||
static constexpr auto KERNEL_CS = 1 * sizeof(gdt_entry);
|
|
||||||
static constexpr auto KERNEL_DS = 2 * sizeof(gdt_entry);
|
|
||||||
static constexpr auto USER_CS = 4 * sizeof(gdt_entry);
|
|
||||||
static constexpr auto USER_DS = 3 * sizeof(gdt_entry);
|
|
||||||
static constexpr auto TSS = 5 * sizeof(gdt_entry);
|
|
||||||
|
|
||||||
gdt(const tss& tss) {
|
|
||||||
// Task State Segment
|
|
||||||
entries[5] = gdt_entry{ static_cast<u32>(reinterpret_cast<u64>(&tss) & 0xFFFFFFFF),
|
|
||||||
sizeof(tss) - 1,
|
|
||||||
gdt_entry::ACCESS_P | gdt_entry::ACCESS_E | gdt_entry::ACCESS_A,
|
|
||||||
0 };
|
|
||||||
entries[6] = gdt_entry{ static_cast<u32>(reinterpret_cast<u64>(&tss) >> 32) };
|
|
||||||
}
|
|
||||||
|
|
||||||
void load();
|
|
||||||
};
|
|
||||||
|
|
||||||
extern "C" void reload_segments(u64 codeSegment, u64 dataSegment);
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
|
|
||||||
#endif // ARCH_X86_64_GDT_HPP
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
#include "./idt.hpp"
|
|
||||||
|
|
||||||
#include "./gdt.hpp"
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
struct idtr {
|
|
||||||
u16 limit;
|
|
||||||
const void* base;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
extern "C" void exception_handler(void) {
|
|
||||||
asm volatile("cli; hlt");
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" void* isrStubTable[256];
|
|
||||||
|
|
||||||
idt::idt()
|
|
||||||
: entries() {
|
|
||||||
for (u32 vector = 0; vector < 256; ++vector) {
|
|
||||||
entries[vector] = idt_entry{
|
|
||||||
static_cast<u16>(reinterpret_cast<u64>(isrStubTable[vector]) & 0xFFFF),
|
|
||||||
gdt::KERNEL_CS,
|
|
||||||
0,
|
|
||||||
0x8E,
|
|
||||||
static_cast<u16>((reinterpret_cast<u64>(isrStubTable[vector]) >> 16) & 0xFFFF),
|
|
||||||
static_cast<u32>((reinterpret_cast<u64>(isrStubTable[vector]) >> 32) & 0xFFFFFFFF),
|
|
||||||
0,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void idt::load() {
|
|
||||||
idtr idtr{ (sizeof(entries) - 1), entries };
|
|
||||||
|
|
||||||
asm volatile("cli\n"
|
|
||||||
"lidt %0" ::"m"(idtr)
|
|
||||||
: "memory");
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
#ifndef ARCH_X86_64_IDT_HPP
|
|
||||||
#define ARCH_X86_64_IDT_HPP
|
|
||||||
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
struct idt_entry {
|
|
||||||
u16 offsetLow;
|
|
||||||
u16 selector;
|
|
||||||
u8 ist;
|
|
||||||
u8 attributes;
|
|
||||||
u16 offsetMid;
|
|
||||||
u32 offsetHigh;
|
|
||||||
u32 rsvd;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
struct idt {
|
|
||||||
idt_entry entries[256];
|
|
||||||
|
|
||||||
idt();
|
|
||||||
|
|
||||||
void load();
|
|
||||||
};
|
|
||||||
|
|
||||||
using vector_t = u8;
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
|
|
||||||
#endif // ARCH_X86_64_IDT_HPP
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
#include "int.hpp"
|
|
||||||
|
|
||||||
#include "arch/x86_64/arch.hpp"
|
|
||||||
#include "arch/x86_64/memory.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
extern "C" void interrupt_handler(interrupt_frame& frame) {
|
|
||||||
if (frame.vector < 0x20) {
|
|
||||||
} else {
|
|
||||||
arch::get().get_irq_manager().dispatch(frame);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
irq_manager::irq_manager(vm& vm)
|
|
||||||
: m_vm(&vm), m_vectorMap() {
|
|
||||||
m_head = m_tail = nullptr;
|
|
||||||
|
|
||||||
for (u64 i = 0; i < 0x100; ++i) {
|
|
||||||
m_vectorMap[i] = INVALID_IRQ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
irq_t irq_manager::allocate() {
|
|
||||||
auto* entry = at(m_count);
|
|
||||||
if (entry == nullptr) {
|
|
||||||
page* newPage = m_vm->allocate<page>();
|
|
||||||
if (newPage == nullptr) return INVALID_IRQ;
|
|
||||||
*newPage = {};
|
|
||||||
|
|
||||||
if (m_head == nullptr)
|
|
||||||
m_head = newPage;
|
|
||||||
else
|
|
||||||
m_tail->next = newPage;
|
|
||||||
m_tail = newPage;
|
|
||||||
|
|
||||||
entry = newPage->entries;
|
|
||||||
}
|
|
||||||
*entry = {};
|
|
||||||
return m_count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool irq_manager::bind(irq_t irq, interrupt_controller& controller, hwirq_t hwirq, vector_t vector) {
|
|
||||||
auto* entry = at(irq);
|
|
||||||
if (entry == nullptr) return false;
|
|
||||||
entry->controller = &controller;
|
|
||||||
entry->hwirq = hwirq;
|
|
||||||
|
|
||||||
entry->vector = vector;
|
|
||||||
m_vectorMap[entry->vector] = irq;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool irq_manager::register_handler(irq_t irq, irq_handler handler, void* data) {
|
|
||||||
auto* entry = at(irq);
|
|
||||||
if (entry == nullptr) return false;
|
|
||||||
entry->handler = handler;
|
|
||||||
entry->data = data;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool irq_manager::enable(irq_t irq) {
|
|
||||||
auto* entry = at(irq);
|
|
||||||
if (entry == nullptr) return false;
|
|
||||||
entry->enabled = true;
|
|
||||||
entry->controller->enable(entry->hwirq);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool irq_manager::disable(irq_t irq) {
|
|
||||||
auto* entry = at(irq);
|
|
||||||
if (entry == nullptr) return false;
|
|
||||||
entry->enabled = false;
|
|
||||||
entry->controller->disable(entry->hwirq);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool irq_manager::dispatch(interrupt_frame& frame) {
|
|
||||||
if (frame.vector >= 0x100) return false;
|
|
||||||
irq_t irq = m_vectorMap[frame.vector];
|
|
||||||
if (irq == INVALID_IRQ) return false;
|
|
||||||
auto* entry = at(irq);
|
|
||||||
if (entry == nullptr || !entry->enabled) return false;
|
|
||||||
|
|
||||||
entry->handler(frame, entry->data);
|
|
||||||
entry->controller->eoi(entry->hwirq);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
irq_manager::descriptor* irq_manager::at(irq_t irq) {
|
|
||||||
u64 cntr = irq / page::ENTRIES;
|
|
||||||
|
|
||||||
page* curPage = m_head;
|
|
||||||
while (curPage != nullptr && cntr-- > 0)
|
|
||||||
curPage = curPage->next;
|
|
||||||
|
|
||||||
return (curPage == nullptr) ? nullptr : &curPage->entries[irq % page::ENTRIES];
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
#ifndef ARCH_X86_64_INT_HPP
|
|
||||||
#define ARCH_X86_64_INT_HPP
|
|
||||||
|
|
||||||
#include "arch/x86_64/idt.hpp"
|
|
||||||
#include "arch/x86_64/memory.hpp"
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
using hwirq_t = u32;
|
|
||||||
static constexpr hwirq_t INVALID_HWIRQ = -1;
|
|
||||||
|
|
||||||
using irq_t = u32;
|
|
||||||
static constexpr irq_t INVALID_IRQ = -1;
|
|
||||||
|
|
||||||
struct interrupt_controller {
|
|
||||||
interrupt_controller() = default;
|
|
||||||
virtual ~interrupt_controller() = default;
|
|
||||||
|
|
||||||
virtual void enable(hwirq_t irq) = 0;
|
|
||||||
virtual void disable(hwirq_t irq) = 0;
|
|
||||||
virtual void eoi(hwirq_t irq) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct interrupt_frame {
|
|
||||||
u64 r15;
|
|
||||||
u64 r14;
|
|
||||||
u64 r13;
|
|
||||||
u64 r12;
|
|
||||||
u64 r11;
|
|
||||||
u64 r10;
|
|
||||||
u64 r9;
|
|
||||||
u64 r8;
|
|
||||||
u64 rbp;
|
|
||||||
u64 rdi;
|
|
||||||
u64 rsi;
|
|
||||||
u64 rdx;
|
|
||||||
u64 rcx;
|
|
||||||
u64 rbx;
|
|
||||||
u64 rax;
|
|
||||||
|
|
||||||
u64 vector;
|
|
||||||
u64 errorCode;
|
|
||||||
|
|
||||||
u64 rip;
|
|
||||||
u64 cs;
|
|
||||||
u64 rflags;
|
|
||||||
u64 rsp;
|
|
||||||
u64 ss;
|
|
||||||
};
|
|
||||||
|
|
||||||
using irq_handler = void (*)(interrupt_frame&, void*);
|
|
||||||
|
|
||||||
class irq_manager {
|
|
||||||
public:
|
|
||||||
irq_manager(vm& vm);
|
|
||||||
|
|
||||||
irq_t allocate();
|
|
||||||
bool bind(irq_t irq, interrupt_controller& controller, hwirq_t hwirq, vector_t vector);
|
|
||||||
bool register_handler(irq_t irq, irq_handler handler, void* data);
|
|
||||||
bool enable(irq_t irq);
|
|
||||||
bool disable(irq_t irq);
|
|
||||||
|
|
||||||
bool dispatch(interrupt_frame& frame);
|
|
||||||
private:
|
|
||||||
struct descriptor {
|
|
||||||
interrupt_controller* controller = nullptr;
|
|
||||||
irq_handler handler = nullptr;
|
|
||||||
void* data = nullptr;
|
|
||||||
|
|
||||||
hwirq_t hwirq = INVALID_HWIRQ;
|
|
||||||
vector_t vector = 0;
|
|
||||||
bool enabled = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct page {
|
|
||||||
static constexpr u64 ENTRIES = (4096 - sizeof(page*)) / sizeof(descriptor);
|
|
||||||
|
|
||||||
page* next = nullptr;
|
|
||||||
descriptor entries[ENTRIES];
|
|
||||||
};
|
|
||||||
|
|
||||||
static_assert(sizeof(page) < 4096, "page struct is too big!");
|
|
||||||
private:
|
|
||||||
descriptor* at(irq_t irq);
|
|
||||||
private:
|
|
||||||
vm* m_vm;
|
|
||||||
irq_t m_count = 0;
|
|
||||||
page* m_head;
|
|
||||||
page* m_tail;
|
|
||||||
|
|
||||||
irq_t m_vectorMap[0x100];
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
|
|
||||||
#endif // ARCH_X86_64_INT_HPP
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#ifndef ARCH_X86_64_IO_HPP
|
|
||||||
#define ARCH_X86_64_IO_HPP
|
|
||||||
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
struct io {
|
|
||||||
static u8 inb(u16 port) {
|
|
||||||
u8 value{};
|
|
||||||
asm volatile("inb %1, %0" : "=a"(value) : "Nd"(port));
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void outb(u16 port, u8 value) { asm volatile("outb %0, %1" : : "a"(value), "Nd"(port)); }
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
|
|
||||||
#endif // ARCH_X86_64_IO_HPP
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
.altmacro
|
|
||||||
|
|
||||||
.section .text
|
|
||||||
.code64
|
|
||||||
|
|
||||||
.extern interrupt_handler
|
|
||||||
|
|
||||||
common_isr_stub:
|
|
||||||
push %rax
|
|
||||||
push %rbx
|
|
||||||
push %rcx
|
|
||||||
push %rdx
|
|
||||||
push %rsi
|
|
||||||
push %rdi
|
|
||||||
push %rbp
|
|
||||||
push %r8
|
|
||||||
push %r9
|
|
||||||
push %r10
|
|
||||||
push %r11
|
|
||||||
push %r12
|
|
||||||
push %r13
|
|
||||||
push %r14
|
|
||||||
push %r15
|
|
||||||
|
|
||||||
cld
|
|
||||||
mov %rsp, %rdi
|
|
||||||
|
|
||||||
mov %rsp, %r12
|
|
||||||
and $-16, %rsp
|
|
||||||
|
|
||||||
call interrupt_handler
|
|
||||||
|
|
||||||
mov %r12, %rsp
|
|
||||||
|
|
||||||
pop %r15
|
|
||||||
pop %r14
|
|
||||||
pop %r13
|
|
||||||
pop %r12
|
|
||||||
pop %r11
|
|
||||||
pop %r10
|
|
||||||
pop %r9
|
|
||||||
pop %r8
|
|
||||||
pop %rbp
|
|
||||||
pop %rdi
|
|
||||||
pop %rsi
|
|
||||||
pop %rdx
|
|
||||||
pop %rcx
|
|
||||||
pop %rbx
|
|
||||||
pop %rax
|
|
||||||
|
|
||||||
add $16, %rsp
|
|
||||||
|
|
||||||
iretq
|
|
||||||
|
|
||||||
.macro isr_err_stub i=1
|
|
||||||
isr_stub_\i:
|
|
||||||
pushq $\i
|
|
||||||
|
|
||||||
jmp common_isr_stub
|
|
||||||
.endm
|
|
||||||
|
|
||||||
.macro isr_nerr_stub i
|
|
||||||
isr_stub_\i:
|
|
||||||
pushq $0
|
|
||||||
pushq $\i
|
|
||||||
|
|
||||||
jmp common_isr_stub
|
|
||||||
.endm
|
|
||||||
|
|
||||||
# From https://wiki.osdev.org/Interrupts_Tutorial
|
|
||||||
isr_nerr_stub 0
|
|
||||||
isr_nerr_stub 1
|
|
||||||
isr_nerr_stub 2
|
|
||||||
isr_nerr_stub 3
|
|
||||||
isr_nerr_stub 4
|
|
||||||
isr_nerr_stub 5
|
|
||||||
isr_nerr_stub 6
|
|
||||||
isr_nerr_stub 7
|
|
||||||
isr_err_stub 8
|
|
||||||
isr_nerr_stub 9
|
|
||||||
isr_err_stub 10
|
|
||||||
isr_err_stub 11
|
|
||||||
isr_err_stub 12
|
|
||||||
isr_err_stub 13
|
|
||||||
isr_err_stub 14
|
|
||||||
isr_nerr_stub 15
|
|
||||||
isr_nerr_stub 16
|
|
||||||
isr_err_stub 17
|
|
||||||
isr_nerr_stub 18
|
|
||||||
isr_nerr_stub 19
|
|
||||||
isr_nerr_stub 20
|
|
||||||
isr_err_stub 21
|
|
||||||
isr_nerr_stub 22
|
|
||||||
isr_nerr_stub 23
|
|
||||||
isr_nerr_stub 24
|
|
||||||
isr_nerr_stub 25
|
|
||||||
isr_nerr_stub 26
|
|
||||||
isr_nerr_stub 27
|
|
||||||
isr_nerr_stub 28
|
|
||||||
isr_err_stub 29
|
|
||||||
isr_err_stub 30
|
|
||||||
isr_nerr_stub 31
|
|
||||||
|
|
||||||
.set i, 32
|
|
||||||
.rept 256 - 32
|
|
||||||
isr_nerr_stub %i
|
|
||||||
.set i, i+1
|
|
||||||
.endr
|
|
||||||
|
|
||||||
.macro isr_label i
|
|
||||||
.quad isr_stub_\i
|
|
||||||
.endm
|
|
||||||
|
|
||||||
.global isrStubTable
|
|
||||||
isrStubTable:
|
|
||||||
.set i, 0
|
|
||||||
.rept 256
|
|
||||||
isr_label %i
|
|
||||||
.set i, i+1
|
|
||||||
.endr
|
|
||||||
@@ -1,230 +0,0 @@
|
|||||||
#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 virtaddr_t NULLADDR = 0;
|
|
||||||
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 {
|
|
||||||
static constexpr virtaddr_t HHDM_BASE = 0xFFFF800000000000ULL;
|
|
||||||
|
|
||||||
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;
|
|
||||||
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(pmm& pmm);
|
|
||||||
|
|
||||||
virtaddr_t reserve(u64 size);
|
|
||||||
bool commit(virtaddr_t addr, u64 size, u64 flags);
|
|
||||||
bool uncommit(virtaddr_t addr, u64 size);
|
|
||||||
bool release(virtaddr_t addr, u64 size);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
T* allocate(u64 count = 1) {
|
|
||||||
virtaddr_t addr = reserve(sizeof(T) * count);
|
|
||||||
if (addr == 0) return nullptr;
|
|
||||||
if (!commit(addr, sizeof(T) * count, paging::PAGE_XD | paging::PAGE_WRITABLE)) {
|
|
||||||
release(addr, sizeof(T) * count);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return reinterpret_cast<T*>(addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void free(T* ptr, u64 count) {
|
|
||||||
virtaddr_t addr = reinterpret_cast<virtaddr_t>(ptr);
|
|
||||||
if (!uncommit(addr, sizeof(T) * count)) return;
|
|
||||||
release(addr, sizeof(T) * count);
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
static constexpr u64 INIT_START = 0xFFFF900000000000ULL;
|
|
||||||
static constexpr u64 INIT_END = 0xFFFFA00000000000ULL;
|
|
||||||
private:
|
|
||||||
struct node {
|
|
||||||
enum color_e {
|
|
||||||
RED = 0,
|
|
||||||
BLACK = 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
// NOTE: The parent node is used for the last free node for first free node.
|
|
||||||
node* parent = nullptr;
|
|
||||||
node* left = nullptr;
|
|
||||||
// NOTE: The right node is used for the next free node in free nodes list.
|
|
||||||
node* right = nullptr;
|
|
||||||
|
|
||||||
u64 base = 0;
|
|
||||||
u64 size = 0;
|
|
||||||
u64 maxSize = 0;
|
|
||||||
|
|
||||||
color_e color = BLACK;
|
|
||||||
|
|
||||||
bool is_red() const { return color == RED; }
|
|
||||||
|
|
||||||
void make_red() { color = RED; }
|
|
||||||
void make_black() { color = BLACK; }
|
|
||||||
|
|
||||||
static u64 max_size(node* node) { return (node != nullptr) ? node->maxSize : 0; }
|
|
||||||
|
|
||||||
void recalculate() {
|
|
||||||
maxSize = max_size(left);
|
|
||||||
if (max_size(right) > maxSize) maxSize = max_size(right);
|
|
||||||
if (size > maxSize) maxSize = size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void recalculate_upwards() {
|
|
||||||
node* node = this;
|
|
||||||
while (node != nullptr) {
|
|
||||||
node->recalculate();
|
|
||||||
node = node->parent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
node* min() {
|
|
||||||
node* node = this;
|
|
||||||
while (node != nullptr && node->left != nullptr)
|
|
||||||
node = node->left;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
node* max() {
|
|
||||||
node* node = this;
|
|
||||||
while (node != nullptr && node->right != nullptr)
|
|
||||||
node = node->right;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
node* pred() {
|
|
||||||
if (left != nullptr) return left->max();
|
|
||||||
node* parent = this->parent;
|
|
||||||
node* node = this;
|
|
||||||
while (parent != nullptr && node == parent->left) {
|
|
||||||
node = parent;
|
|
||||||
parent = parent->parent;
|
|
||||||
}
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct page {
|
|
||||||
static constexpr u64 ENTRY_COUNT = 0x1000 / sizeof(node);
|
|
||||||
|
|
||||||
node nodes[ENTRY_COUNT];
|
|
||||||
};
|
|
||||||
private:
|
|
||||||
void insert(node* node);
|
|
||||||
void insert_fixup(node* node);
|
|
||||||
void transplant(node* first, node* second);
|
|
||||||
void remove(node* node);
|
|
||||||
void remove_fixup(node* first, node* second);
|
|
||||||
|
|
||||||
node* lower_bound(u64 base);
|
|
||||||
node* find_first_fit(u64 size);
|
|
||||||
|
|
||||||
void rotate_left(node* node);
|
|
||||||
void rotate_right(node* node);
|
|
||||||
private:
|
|
||||||
node* allocate_node();
|
|
||||||
void free_node(node* node);
|
|
||||||
|
|
||||||
// NOTE: This function allocates a page full of free nodes and returns a pointer to the first one (that is not
|
|
||||||
// tracked in the free nodes linked list).
|
|
||||||
node* allocate_page();
|
|
||||||
private:
|
|
||||||
pmm* m_pmm = nullptr;
|
|
||||||
node* m_firstFree = nullptr;
|
|
||||||
node* m_root = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
|
|
||||||
#endif // ARCH_X86_64_MEMORY_HPP
|
|
||||||
@@ -1,271 +0,0 @@
|
|||||||
#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);
|
|
||||||
|
|
||||||
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 (entryEnd < BOOTSTRAP_HHDM_LIMIT) continue;
|
|
||||||
|
|
||||||
map_hhdm(begin >= BOOTSTRAP_HHDM_LIMIT ? begin : BOOTSTRAP_HHDM_LIMIT, entryEnd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
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) return INVALID_PHYSADDR;
|
|
||||||
|
|
||||||
u64* pdpt = tableFromPhys(pml4e & PHYS_MASK);
|
|
||||||
u64 pdpte = pdpt[pdptIdx];
|
|
||||||
if ((pdpte & PAGE_PRESENT) == 0) return INVALID_PHYSADDR;
|
|
||||||
|
|
||||||
u64* pd = tableFromPhys(pdpte & PHYS_MASK);
|
|
||||||
u64 pde = pd[pdIdx];
|
|
||||||
if ((pde & PAGE_PRESENT) == 0) return INVALID_PHYSADDR;
|
|
||||||
if ((pde & PAGE_LARGE) == PAGE_LARGE) panic();
|
|
||||||
|
|
||||||
u64* pt = tableFromPhys(pde & PHYS_MASK);
|
|
||||||
u64 pte = pt[ptIdx];
|
|
||||||
|
|
||||||
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
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
#ifndef ARCH_X86_64_PIC_HPP
|
|
||||||
#define ARCH_X86_64_PIC_HPP
|
|
||||||
|
|
||||||
#include "./io.hpp"
|
|
||||||
#include "arch/x86_64/int.hpp"
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
struct pic : interrupt_controller {
|
|
||||||
static constexpr u16 PIC1_BASE = 0x20;
|
|
||||||
static constexpr u16 PIC2_BASE = 0xA0;
|
|
||||||
|
|
||||||
static constexpr u16 PIC1_CMD = PIC1_BASE + 0;
|
|
||||||
static constexpr u16 PIC1_DATA = PIC1_BASE + 1;
|
|
||||||
static constexpr u16 PIC2_CMD = PIC2_BASE + 0;
|
|
||||||
static constexpr u16 PIC2_DATA = PIC2_BASE + 1;
|
|
||||||
|
|
||||||
void remap(u8 off1, u8 off2) {
|
|
||||||
static constexpr u8 ICW1_ICW4 = 0x01;
|
|
||||||
static constexpr u8 ICW1_INIT = 0x10;
|
|
||||||
|
|
||||||
static constexpr u8 CASCADE_IRQ = 2;
|
|
||||||
|
|
||||||
static constexpr u8 ICW4_8086 = 0x01;
|
|
||||||
|
|
||||||
io::outb(PIC1_CMD, ICW1_INIT | ICW1_ICW4);
|
|
||||||
io::outb(PIC2_CMD, ICW1_INIT | ICW1_ICW4);
|
|
||||||
io::outb(PIC1_DATA, off1);
|
|
||||||
io::outb(PIC2_DATA, off2);
|
|
||||||
io::outb(PIC1_DATA, 1 << CASCADE_IRQ);
|
|
||||||
io::outb(PIC2_DATA, CASCADE_IRQ);
|
|
||||||
io::outb(PIC1_DATA, ICW4_8086);
|
|
||||||
io::outb(PIC2_DATA, ICW4_8086);
|
|
||||||
io::outb(PIC1_DATA, 0xFB);
|
|
||||||
io::outb(PIC2_DATA, 0xFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void disable_pic(void) {
|
|
||||||
io::outb(PIC1_DATA, 0xFF);
|
|
||||||
io::outb(PIC2_DATA, 0xFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
void enable(hwirq_t irq) override {
|
|
||||||
u16 port = (irq < 8) ? PIC1_DATA : PIC2_DATA;
|
|
||||||
io::outb(port, io::inb(port) & ~(1 << (irq & 7)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void disable(hwirq_t irq) override {
|
|
||||||
u16 port = (irq < 8) ? PIC1_DATA : PIC2_DATA;
|
|
||||||
io::outb(port, io::inb(port) | (1 << (irq & 7)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void eoi(hwirq_t irq) override {
|
|
||||||
static constexpr u8 EOI = 0x20;
|
|
||||||
if (irq >= 8) io::outb(PIC2_CMD, EOI);
|
|
||||||
io::outb(PIC1_CMD, EOI);
|
|
||||||
}
|
|
||||||
|
|
||||||
u16 get_irr(void) {
|
|
||||||
static constexpr u8 READ_IRR = 0x0A;
|
|
||||||
io::outb(PIC1_CMD, READ_IRR);
|
|
||||||
io::outb(PIC2_CMD, READ_IRR);
|
|
||||||
return (io::inb(PIC2_CMD) << 8) | io::inb(PIC1_CMD);
|
|
||||||
}
|
|
||||||
|
|
||||||
u16 get_isr(void) {
|
|
||||||
static constexpr u8 READ_ISR = 0x0B;
|
|
||||||
io::outb(PIC1_CMD, READ_ISR);
|
|
||||||
io::outb(PIC2_CMD, READ_ISR);
|
|
||||||
return (io::inb(PIC2_CMD) << 8) | io::inb(PIC1_CMD);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
|
|
||||||
#endif // ARCH_X86_64_PIC_HPP
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
#ifndef ARCH_X86_64_PIT_HPP
|
|
||||||
#define ARCH_X86_64_PIT_HPP
|
|
||||||
|
|
||||||
#include "arch/x86_64/int.hpp"
|
|
||||||
#include "arch/x86_64/io.hpp"
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
struct pit {
|
|
||||||
pit(u32 targetHz = 100) {
|
|
||||||
constexpr u32 PIT_INPUT_HZ = 1193182;
|
|
||||||
|
|
||||||
const u16 divisor = static_cast<u16>(PIT_INPUT_HZ / targetHz);
|
|
||||||
|
|
||||||
io::outb(0x43, 0x36);
|
|
||||||
|
|
||||||
io::outb(0x40, static_cast<u8>(divisor & 0xFF));
|
|
||||||
io::outb(0x40, static_cast<u8>(divisor >> 8));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handler(interrupt_frame& frame, void* data) {
|
|
||||||
(void)frame;
|
|
||||||
|
|
||||||
pit* pit = reinterpret_cast<decltype(pit)>(data);
|
|
||||||
++pit->ticks;
|
|
||||||
}
|
|
||||||
|
|
||||||
volatile u64 ticks = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
|
|
||||||
#endif // ARCH_X86_64_PIT_HPP
|
|
||||||
@@ -1,172 +0,0 @@
|
|||||||
#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,28 +0,0 @@
|
|||||||
#ifndef ARCH_X86_64_TSS_HPP
|
|
||||||
#define ARCH_X86_64_TSS_HPP
|
|
||||||
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
struct tss {
|
|
||||||
u32 rsv0;
|
|
||||||
u64 rsp0;
|
|
||||||
u64 rsp1;
|
|
||||||
u64 rsp2;
|
|
||||||
u64 rsv1;
|
|
||||||
u64 ist1;
|
|
||||||
u64 ist2;
|
|
||||||
u64 ist3;
|
|
||||||
u64 ist4;
|
|
||||||
u64 ist5;
|
|
||||||
u64 ist6;
|
|
||||||
u64 ist7;
|
|
||||||
u64 rsv2;
|
|
||||||
u16 rsv3;
|
|
||||||
u16 iopb;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
|
|
||||||
#endif // ARCH_X86_64_TSS_HPP
|
|
||||||
@@ -1,535 +0,0 @@
|
|||||||
#include "./memory.hpp"
|
|
||||||
#include "arch/x86_64/arch.hpp"
|
|
||||||
#include "vga.hpp"
|
|
||||||
|
|
||||||
namespace x86_64 {
|
|
||||||
|
|
||||||
vm::vm(pmm& pmm)
|
|
||||||
: m_pmm(&pmm), m_root(allocate_node()) {
|
|
||||||
static_assert((INIT_START & 0x1000) == 0);
|
|
||||||
static_assert((INIT_END & 0x1000) == 0);
|
|
||||||
static_assert(INIT_START < INIT_END);
|
|
||||||
|
|
||||||
if (m_root == nullptr) {
|
|
||||||
vga::puts("Failed to allocate memory for Virtual Address Allocator!!!", 0, 0);
|
|
||||||
asm volatile("cli; hlt");
|
|
||||||
}
|
|
||||||
|
|
||||||
m_root->base = INIT_START;
|
|
||||||
m_root->maxSize = m_root->size = INIT_END - INIT_START;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtaddr_t vm::reserve(u64 size) {
|
|
||||||
size = (size + 0xFFF) & ~0xFFF;
|
|
||||||
|
|
||||||
auto* node = find_first_fit(size);
|
|
||||||
if (node == nullptr) return NULLADDR;
|
|
||||||
u64 result = node->base;
|
|
||||||
|
|
||||||
if (node->size == size) {
|
|
||||||
remove(node);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->base += size;
|
|
||||||
node->size -= size;
|
|
||||||
node->recalculate_upwards();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool vm::commit(virtaddr_t addr, u64 size, u64 flags) {
|
|
||||||
const u64 pageEnd = (addr + size + 0xFFFULL) >> 12ULL;
|
|
||||||
|
|
||||||
u64 page = addr >> 12ULL;
|
|
||||||
for (; page != pageEnd; ++page) {
|
|
||||||
auto alloced = arch::get().get_pmm().alloc_page();
|
|
||||||
if (alloced == INVALID_PHYSADDR) break;
|
|
||||||
|
|
||||||
arch::get().get_paging().map(alloced, page << 12ULL, flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (page == pageEnd) return true;
|
|
||||||
|
|
||||||
// Roll-back on failure
|
|
||||||
for (u64 cur = addr >> 12ULL; cur != page; ++cur) {
|
|
||||||
arch::get().get_pmm().free_page(arch::get().get_paging().get(cur << 12ULL));
|
|
||||||
|
|
||||||
arch::get().get_paging().unmap(cur << 12ULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool vm::uncommit(virtaddr_t addr, u64 size) {
|
|
||||||
const u64 pageEnd = (addr + size + 0xFFFULL) >> 12ULL;
|
|
||||||
|
|
||||||
for (u64 page = addr >> 12ULL; page != pageEnd; ++page) {
|
|
||||||
physaddr_t paddr = arch::get().get_paging().get(page << 12ULL);
|
|
||||||
if (paddr == INVALID_PHYSADDR) return false;
|
|
||||||
arch::get().get_pmm().free_page(paddr);
|
|
||||||
|
|
||||||
arch::get().get_paging().unmap(page << 12ULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool vm::release(virtaddr_t addr, u64 size) {
|
|
||||||
if (size == 0) return false;
|
|
||||||
|
|
||||||
const u64 pageStart = addr & ~0xFFFULL;
|
|
||||||
const u64 pageEnd = (addr + size + 0xFFF) & ~0xFFFULL;
|
|
||||||
if (pageStart == pageEnd) return false;
|
|
||||||
if (pageEnd < pageStart) return false;
|
|
||||||
if (pageStart < INIT_START) return false;
|
|
||||||
if (pageEnd > INIT_END) return false;
|
|
||||||
|
|
||||||
node* next = lower_bound(pageStart);
|
|
||||||
node* prev = nullptr;
|
|
||||||
if (next != nullptr)
|
|
||||||
prev = next->pred();
|
|
||||||
else if (m_root != nullptr)
|
|
||||||
prev = m_root->max();
|
|
||||||
|
|
||||||
if (prev != nullptr && prev->base + prev->size > pageStart) return false;
|
|
||||||
if (next != nullptr && pageEnd > next->base) return false;
|
|
||||||
|
|
||||||
const bool mergePrev = prev != nullptr && prev->base + prev->size == pageStart;
|
|
||||||
const bool mergeNext = next != nullptr && pageEnd == next->base;
|
|
||||||
|
|
||||||
if (mergePrev && mergeNext) {
|
|
||||||
prev->size += (pageEnd - pageStart) + next->size;
|
|
||||||
remove(next);
|
|
||||||
prev->recalculate_upwards();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mergePrev) {
|
|
||||||
prev->size += (pageEnd - pageStart);
|
|
||||||
prev->recalculate_upwards();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mergeNext) {
|
|
||||||
next->base = pageStart;
|
|
||||||
next->size += (pageEnd - pageStart);
|
|
||||||
next->recalculate_upwards();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
node* node = allocate_node();
|
|
||||||
if (node == nullptr) return false;
|
|
||||||
node->base = pageStart;
|
|
||||||
node->maxSize = node->size = (pageEnd - pageStart);
|
|
||||||
node->make_red();
|
|
||||||
|
|
||||||
insert(node);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void vm::insert(node* node) {
|
|
||||||
decltype(node) prn = nullptr;
|
|
||||||
decltype(node) cur = m_root;
|
|
||||||
while (cur != nullptr) {
|
|
||||||
prn = cur;
|
|
||||||
cur = node->base < cur->base ? cur->left : cur->right;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->parent = prn;
|
|
||||||
|
|
||||||
if (prn == nullptr) {
|
|
||||||
m_root = node;
|
|
||||||
} else if (node->base < prn->base) {
|
|
||||||
prn->left = node;
|
|
||||||
} else {
|
|
||||||
prn->right = node;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->left = node->right = nullptr;
|
|
||||||
node->make_red();
|
|
||||||
node->recalculate_upwards();
|
|
||||||
insert_fixup(node);
|
|
||||||
node->recalculate_upwards();
|
|
||||||
}
|
|
||||||
|
|
||||||
void vm::insert_fixup(node* node) {
|
|
||||||
while (node->parent != nullptr && node->parent->is_red()) {
|
|
||||||
auto* parent = node->parent;
|
|
||||||
auto* grandp = parent->parent;
|
|
||||||
if (parent == grandp->left) {
|
|
||||||
auto* uncle = grandp->right;
|
|
||||||
|
|
||||||
if (uncle != nullptr && uncle->is_red()) {
|
|
||||||
parent->make_black();
|
|
||||||
uncle->make_black();
|
|
||||||
grandp->make_red();
|
|
||||||
node = grandp;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node == parent->right) {
|
|
||||||
node = parent;
|
|
||||||
rotate_left(node);
|
|
||||||
parent = node->parent;
|
|
||||||
grandp = parent != nullptr ? parent->parent : nullptr;
|
|
||||||
}
|
|
||||||
parent->make_black();
|
|
||||||
grandp->make_red();
|
|
||||||
rotate_right(grandp);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto* uncle = grandp->left;
|
|
||||||
if (uncle != nullptr && uncle->is_red()) {
|
|
||||||
parent->make_black();
|
|
||||||
uncle->make_black();
|
|
||||||
grandp->make_red();
|
|
||||||
node = grandp;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node == parent->left) {
|
|
||||||
node = parent;
|
|
||||||
rotate_right(node);
|
|
||||||
parent = node->parent;
|
|
||||||
grandp = parent != nullptr ? parent->parent : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
parent->make_black();
|
|
||||||
grandp->make_red();
|
|
||||||
rotate_left(grandp);
|
|
||||||
}
|
|
||||||
|
|
||||||
m_root->make_black();
|
|
||||||
}
|
|
||||||
|
|
||||||
void vm::transplant(node* first, node* second) {
|
|
||||||
if (first->parent == nullptr) {
|
|
||||||
m_root = second;
|
|
||||||
} else if (first == first->parent->left) {
|
|
||||||
first->parent->left = second;
|
|
||||||
} else {
|
|
||||||
first->parent->right = second;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (second != nullptr) {
|
|
||||||
second->parent = first->parent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void vm::remove(node* node) {
|
|
||||||
auto* oth = node;
|
|
||||||
|
|
||||||
const bool origRed = oth->is_red();
|
|
||||||
decltype(node) xNode = nullptr;
|
|
||||||
decltype(node) xPrnt = nullptr;
|
|
||||||
|
|
||||||
if (node->left == nullptr) {
|
|
||||||
xNode = node->right;
|
|
||||||
xPrnt = node->parent;
|
|
||||||
transplant(node, node->right);
|
|
||||||
if (xNode != nullptr) {
|
|
||||||
xPrnt = xNode->parent;
|
|
||||||
}
|
|
||||||
if (xPrnt != nullptr) xPrnt->recalculate_upwards();
|
|
||||||
} else if (node->right == nullptr) {
|
|
||||||
xNode = node->left;
|
|
||||||
xPrnt = node->parent;
|
|
||||||
transplant(node, node->left);
|
|
||||||
if (xNode != nullptr) {
|
|
||||||
xPrnt = xNode->parent;
|
|
||||||
}
|
|
||||||
if (xPrnt != nullptr) xPrnt->recalculate_upwards();
|
|
||||||
} else {
|
|
||||||
oth = node->right->min();
|
|
||||||
const bool othOrigRed = oth->is_red();
|
|
||||||
xNode = oth->right;
|
|
||||||
auto* oldOthPrnt = oth->parent;
|
|
||||||
if (oth->parent == node) {
|
|
||||||
xPrnt = oth;
|
|
||||||
if (xNode != nullptr) xNode->parent = oth;
|
|
||||||
} else {
|
|
||||||
xPrnt = oth->parent;
|
|
||||||
transplant(oth, oth->right);
|
|
||||||
oldOthPrnt->recalculate_upwards();
|
|
||||||
oth->right = node->right;
|
|
||||||
oth->right->parent = oth;
|
|
||||||
}
|
|
||||||
|
|
||||||
transplant(node, oth);
|
|
||||||
|
|
||||||
oth->left = node->left;
|
|
||||||
oth->left->parent = oth;
|
|
||||||
if (node->is_red()) {
|
|
||||||
oth->make_red();
|
|
||||||
} else {
|
|
||||||
oth->make_black();
|
|
||||||
}
|
|
||||||
|
|
||||||
oth->recalculate_upwards();
|
|
||||||
|
|
||||||
if (!othOrigRed) remove_fixup(xNode, xPrnt);
|
|
||||||
|
|
||||||
if (xNode != nullptr) {
|
|
||||||
xNode->recalculate_upwards();
|
|
||||||
} else if (xPrnt != nullptr) {
|
|
||||||
xPrnt->recalculate_upwards();
|
|
||||||
}
|
|
||||||
|
|
||||||
free_node(node);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!origRed) {
|
|
||||||
remove_fixup(xNode, xPrnt);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xNode != nullptr) {
|
|
||||||
xNode->recalculate_upwards();
|
|
||||||
} else if (xPrnt != nullptr) {
|
|
||||||
xPrnt->recalculate_upwards();
|
|
||||||
}
|
|
||||||
|
|
||||||
free_node(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void vm::remove_fixup(node* first, node* second) {
|
|
||||||
while (first != m_root && (first == nullptr || !first->is_red())) {
|
|
||||||
if (second == nullptr) break;
|
|
||||||
if (first == second->left) {
|
|
||||||
node* sibling = second->right;
|
|
||||||
if (sibling->is_red()) {
|
|
||||||
sibling->make_black();
|
|
||||||
second->make_red();
|
|
||||||
rotate_left(second);
|
|
||||||
sibling = second->right;
|
|
||||||
}
|
|
||||||
|
|
||||||
const bool siblingLeftRed = sibling != nullptr ? sibling->left->is_red() : false;
|
|
||||||
const bool siblingRightRed = sibling != nullptr ? sibling->right->is_red() : false;
|
|
||||||
|
|
||||||
if (!siblingLeftRed && !siblingRightRed) {
|
|
||||||
if (sibling != nullptr) sibling->make_red();
|
|
||||||
first = second;
|
|
||||||
second = first->parent;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!siblingRightRed) {
|
|
||||||
if (sibling != nullptr && sibling->left != nullptr) {
|
|
||||||
sibling->left->make_black();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sibling != nullptr) {
|
|
||||||
sibling->make_red();
|
|
||||||
rotate_right(sibling);
|
|
||||||
}
|
|
||||||
|
|
||||||
sibling = second->right;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sibling != nullptr) {
|
|
||||||
if (second->is_red()) {
|
|
||||||
sibling->make_red();
|
|
||||||
} else {
|
|
||||||
sibling->make_black();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
second->make_black();
|
|
||||||
if (sibling != nullptr && sibling->right != nullptr) {
|
|
||||||
sibling->right->make_black();
|
|
||||||
}
|
|
||||||
|
|
||||||
rotate_left(second);
|
|
||||||
|
|
||||||
first = m_root;
|
|
||||||
second = nullptr;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
node* sibling = second->left;
|
|
||||||
|
|
||||||
if (sibling != nullptr && sibling->is_red()) {
|
|
||||||
sibling->make_black();
|
|
||||||
second->make_red();
|
|
||||||
|
|
||||||
rotate_right(second);
|
|
||||||
|
|
||||||
sibling = second->left;
|
|
||||||
}
|
|
||||||
|
|
||||||
const bool siblingRightRed = sibling != nullptr ? sibling->right->is_red() : false;
|
|
||||||
const bool siblingLeftRed = sibling != nullptr ? sibling->left->is_red() : false;
|
|
||||||
|
|
||||||
if (!siblingRightRed && !siblingLeftRed) {
|
|
||||||
if (sibling != nullptr) sibling->make_red();
|
|
||||||
|
|
||||||
first = second;
|
|
||||||
second = first->parent;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!siblingLeftRed) {
|
|
||||||
if (sibling != nullptr && sibling->right != nullptr) {
|
|
||||||
sibling->right->make_black();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sibling != nullptr) {
|
|
||||||
sibling->make_red();
|
|
||||||
|
|
||||||
rotate_left(sibling);
|
|
||||||
}
|
|
||||||
|
|
||||||
sibling = second->left;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sibling != nullptr) {
|
|
||||||
if (second->is_red()) {
|
|
||||||
sibling->make_red();
|
|
||||||
} else {
|
|
||||||
sibling->make_black();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
second->make_black();
|
|
||||||
|
|
||||||
if (sibling != nullptr && sibling->left != nullptr) {
|
|
||||||
sibling->left->make_black();
|
|
||||||
}
|
|
||||||
|
|
||||||
rotate_right(second);
|
|
||||||
|
|
||||||
first = m_root;
|
|
||||||
second = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (first != nullptr) {
|
|
||||||
first->make_black();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vm::node* vm::lower_bound(u64 base) {
|
|
||||||
node* res = nullptr;
|
|
||||||
node* cur = m_root;
|
|
||||||
while (cur != nullptr) {
|
|
||||||
if (cur->base >= base) {
|
|
||||||
res = cur;
|
|
||||||
cur = cur->left;
|
|
||||||
} else {
|
|
||||||
cur = cur->right;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
vm::node* vm::find_first_fit(u64 size) {
|
|
||||||
node* cur = m_root;
|
|
||||||
while (cur != nullptr) {
|
|
||||||
if (cur->left != nullptr && cur->left->maxSize >= size) {
|
|
||||||
cur = cur->left;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cur->size >= size) return cur;
|
|
||||||
|
|
||||||
if (cur->right != nullptr && cur->right->maxSize >= size) {
|
|
||||||
cur = cur->right;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void vm::rotate_left(node* node) {
|
|
||||||
auto* oth = node->right;
|
|
||||||
node->right = oth->left;
|
|
||||||
if (oth->left != nullptr) oth->left->parent = node;
|
|
||||||
oth->parent = node->parent;
|
|
||||||
|
|
||||||
if (node->parent == nullptr) {
|
|
||||||
m_root = oth;
|
|
||||||
} else if (node == node->parent->left) {
|
|
||||||
node->parent->left = oth;
|
|
||||||
} else {
|
|
||||||
node->parent->right = oth;
|
|
||||||
}
|
|
||||||
|
|
||||||
oth->left = node;
|
|
||||||
node->parent = oth;
|
|
||||||
|
|
||||||
node->recalculate();
|
|
||||||
oth->recalculate_upwards();
|
|
||||||
}
|
|
||||||
|
|
||||||
void vm::rotate_right(node* node) {
|
|
||||||
auto* oth = node->left;
|
|
||||||
node->left = oth->right;
|
|
||||||
if (oth->right != nullptr) oth->right->parent = node;
|
|
||||||
oth->parent = node->parent;
|
|
||||||
|
|
||||||
if (node->parent == nullptr) {
|
|
||||||
m_root = oth;
|
|
||||||
} else if (node == node->parent->right) {
|
|
||||||
node->parent->right = oth;
|
|
||||||
} else {
|
|
||||||
node->parent->left = oth;
|
|
||||||
}
|
|
||||||
|
|
||||||
oth->right = node;
|
|
||||||
node->parent = oth;
|
|
||||||
|
|
||||||
node->recalculate();
|
|
||||||
oth->recalculate_upwards();
|
|
||||||
}
|
|
||||||
|
|
||||||
vm::node* vm::allocate_node() {
|
|
||||||
if (m_firstFree != nullptr) {
|
|
||||||
node* node = m_firstFree;
|
|
||||||
m_firstFree = m_firstFree->right;
|
|
||||||
m_firstFree->parent = node->parent;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
return allocate_page();
|
|
||||||
}
|
|
||||||
|
|
||||||
void vm::free_node(node* node) {
|
|
||||||
*node = {};
|
|
||||||
|
|
||||||
if (m_firstFree == nullptr) {
|
|
||||||
m_firstFree = node;
|
|
||||||
node->parent = node;
|
|
||||||
} else {
|
|
||||||
m_firstFree->parent = m_firstFree->parent->right = node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vm::node* vm::allocate_page() {
|
|
||||||
physaddr_t pagePhys = m_pmm->alloc_page();
|
|
||||||
if (pagePhys == INVALID_PHYSADDR) return nullptr;
|
|
||||||
node* first = reinterpret_cast<node*>(pagePhys + paging::HHDM_BASE);
|
|
||||||
*first = {};
|
|
||||||
|
|
||||||
node* last = first + page::ENTRY_COUNT - 1;
|
|
||||||
*last = {};
|
|
||||||
|
|
||||||
node* it = first + 1;
|
|
||||||
*it = {};
|
|
||||||
|
|
||||||
if (m_firstFree == nullptr) m_firstFree = it;
|
|
||||||
|
|
||||||
while (it != last) {
|
|
||||||
it->right = it + 1;
|
|
||||||
it += 1;
|
|
||||||
*it = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
return (m_firstFree->parent = last, first);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace x86_64
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#include "arch/arch.hpp"
|
|
||||||
#include "vga.hpp"
|
|
||||||
|
|
||||||
extern "C" [[noreturn]] void kmain(void) {
|
|
||||||
vga::clear();
|
|
||||||
|
|
||||||
arch::get().init();
|
|
||||||
|
|
||||||
vga::puts("TowOS initialized!", 0, 0);
|
|
||||||
vga::puts("Zdravstvuyte, tovarisch!", 0, 1);
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
__asm__ volatile("hlt");
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
#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
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
#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,51 +0,0 @@
|
|||||||
// NOLINTBEGIN
|
|
||||||
|
|
||||||
extern "C" int atexit(void (*func)(void)) {
|
|
||||||
(void)func;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" int __cxa_atexit(void (*func)(void*), void* ptr, void* dtr) {
|
|
||||||
(void)func;
|
|
||||||
(void)ptr;
|
|
||||||
(void)dtr;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* operator new(unsigned long size, void* ptr) noexcept {
|
|
||||||
(void)size;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* operator new[](unsigned long size, void* ptr) noexcept {
|
|
||||||
(void)size;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator delete(void* ptr) noexcept {
|
|
||||||
(void)ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator delete(void* ptr, unsigned long size) noexcept {
|
|
||||||
(void)ptr;
|
|
||||||
(void)size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator delete[](void* ptr) noexcept {
|
|
||||||
(void)ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator delete[](void* ptr, unsigned long size) noexcept {
|
|
||||||
(void)ptr;
|
|
||||||
(void)size;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" void* memset(void* dest, int ch, unsigned long count) {
|
|
||||||
unsigned char* p = static_cast<unsigned char*>(dest);
|
|
||||||
while (count--) {
|
|
||||||
*p++ = static_cast<unsigned char>(ch);
|
|
||||||
}
|
|
||||||
return dest;
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOLINTEND
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#ifndef RUNTIME_HPP
|
|
||||||
#define RUNTIME_HPP
|
|
||||||
|
|
||||||
void* operator new(unsigned long size, void* ptr) noexcept;
|
|
||||||
void* operator new[](unsigned long size, void* ptr) noexcept;
|
|
||||||
|
|
||||||
#endif // RUNTIME_HPP
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#ifndef TYPES_HPP
|
|
||||||
#define TYPES_HPP
|
|
||||||
|
|
||||||
using u8 = unsigned char;
|
|
||||||
using u16 = unsigned short;
|
|
||||||
using u32 = unsigned int;
|
|
||||||
using u64 = unsigned long long;
|
|
||||||
|
|
||||||
#define PACKED __attribute__((packed))
|
|
||||||
|
|
||||||
#endif // TYPES_HPP
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
#include "vga.hpp"
|
|
||||||
|
|
||||||
static volatile u16* const s_vga = reinterpret_cast<volatile u16*>(0xB8000);
|
|
||||||
|
|
||||||
void vga::clear(void) {
|
|
||||||
for (u32 i = 0; i < ROWS * COLS; ++i)
|
|
||||||
s_vga[i] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void vga::putc(char ch, u8 col, u8 row) {
|
|
||||||
s_vga[col + (COLS * row)] = 0x0700 | ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
void vga::puts(const char* str, u8 col, u8 row) {
|
|
||||||
for (u32 off = 0; str[off] != '\0'; ++off)
|
|
||||||
putc(str[off], col + off, row);
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
#ifndef VGA_HPP
|
|
||||||
#define VGA_HPP
|
|
||||||
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
class vga {
|
|
||||||
public:
|
|
||||||
static constexpr u8 ROWS = 25;
|
|
||||||
static constexpr u8 COLS = 80;
|
|
||||||
public:
|
|
||||||
static void clear();
|
|
||||||
static void putc(char ch, u8 col, u8 row);
|
|
||||||
static void puts(const char* str, u8 col, u8 row);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // VGA_HPP
|
|
||||||
Reference in New Issue
Block a user