feat: HHDM
This commit is contained in:
@@ -61,7 +61,7 @@ int main()
|
|||||||
|
|
||||||
status = gBS->AllocatePool(EfiLoaderData, sizeof(Bootinfo), (void**)&boot_info);
|
status = gBS->AllocatePool(EfiLoaderData, sizeof(Bootinfo), (void**)&boot_info);
|
||||||
|
|
||||||
boot_info->framebuffer.base = (uintn_t*)gop->Mode->FrameBufferBase;
|
boot_info->framebuffer.base = (uint32_t*)gop->Mode->FrameBufferBase;
|
||||||
boot_info->framebuffer.base_size = gop->Mode->FrameBufferSize;
|
boot_info->framebuffer.base_size = gop->Mode->FrameBufferSize;
|
||||||
boot_info->framebuffer.height = gop->Mode->Information->VerticalResolution;
|
boot_info->framebuffer.height = gop->Mode->Information->VerticalResolution;
|
||||||
boot_info->framebuffer.width = gop->Mode->Information->HorizontalResolution;
|
boot_info->framebuffer.width = gop->Mode->Information->HorizontalResolution;
|
||||||
|
|||||||
+2
-2
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
typedef unsigned _BitInt(32) bi_u32;
|
typedef unsigned int bi_u32;
|
||||||
typedef unsigned _BitInt(64) bi_u64;
|
typedef unsigned long long bi_u64;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bi_u32 *base;
|
bi_u32 *base;
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ target_compile_options(kernel PRIVATE
|
|||||||
-fno-stack-protector
|
-fno-stack-protector
|
||||||
-fno-builtin
|
-fno-builtin
|
||||||
-Wall -Wextra
|
-Wall -Wextra
|
||||||
|
-mcmodel=kernel
|
||||||
-g
|
-g
|
||||||
>
|
>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,6 +15,6 @@ typedef struct {
|
|||||||
|
|
||||||
extern kb_buffer kb_buf;
|
extern kb_buffer kb_buf;
|
||||||
|
|
||||||
void kb_handler(Registers *regs);
|
void kb_handler();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+3
-1
@@ -5,6 +5,7 @@
|
|||||||
#define PMM_H
|
#define PMM_H
|
||||||
|
|
||||||
#include "bootinfo.h"
|
#include "bootinfo.h"
|
||||||
|
#include <types.h>
|
||||||
|
|
||||||
#define PAGE_SIZE 4096
|
#define PAGE_SIZE 4096
|
||||||
#define BLOCKS_PER_BYTE 8
|
#define BLOCKS_PER_BYTE 8
|
||||||
@@ -17,7 +18,8 @@
|
|||||||
#define BITMAP_SET(bitmap, addr) (bitmap[BITMAP_BYTE_INDEX(addr)] |= (1 << BITMAP_BIT_OFFSET(addr)))
|
#define BITMAP_SET(bitmap, addr) (bitmap[BITMAP_BYTE_INDEX(addr)] |= (1 << BITMAP_BIT_OFFSET(addr)))
|
||||||
#define BITMAP_UNSET(bitmap, addr) (bitmap[BITMAP_BYTE_INDEX(addr)] &= ~(1 << BITMAP_BIT_OFFSET(addr)))
|
#define BITMAP_UNSET(bitmap, addr) (bitmap[BITMAP_BYTE_INDEX(addr)] &= ~(1 << BITMAP_BIT_OFFSET(addr)))
|
||||||
|
|
||||||
void pmm_init(BI_MemoryMap mmap);
|
u64 pmm_get_total_mem();
|
||||||
|
void pmm_init(BI_MemoryMap* mmap);
|
||||||
void* pmm_alloc_page();
|
void* pmm_alloc_page();
|
||||||
void pmm_free_page(void* addr);
|
void pmm_free_page(void* addr);
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,14 @@
|
|||||||
#define VMM_PDPT_INDEX(virt) (((virt) >> 30) & 0x1FF) // PDPT Index (bits 30-38)
|
#define VMM_PDPT_INDEX(virt) (((virt) >> 30) & 0x1FF) // PDPT Index (bits 30-38)
|
||||||
#define VMM_PML4_INDEX(virt) (((virt) >> 39) & 0x1FF) // PML4 Index (bits 39-47): main page
|
#define VMM_PML4_INDEX(virt) (((virt) >> 39) & 0x1FF) // PML4 Index (bits 39-47): main page
|
||||||
|
|
||||||
|
|
||||||
|
#define KERNEL_VMA 0xFFFFFFFF80000000
|
||||||
|
#define HHDM_OFFSET 0xFFFF888000000000
|
||||||
|
#define FB_VIRT_BASE 0xFFFFFFFFFC000000
|
||||||
|
#define KERNEL_VIRT_TO_PHYS(virt) ((virt) - KERNEL_VMA)
|
||||||
|
#define PHYS_TO_HHDM(phys) ((phys) + HHDM_OFFSET)
|
||||||
|
#define HHDM_TO_PHYS(virt) ((virt) - HHDM_OFFSET)
|
||||||
|
|
||||||
void vmm_init(Bootinfo* info);
|
void vmm_init(Bootinfo* info);
|
||||||
void vmm_map_page(u64* pml4, u64 phys, u64 virt, u64 flags);
|
void vmm_map_page(u64* pml4, u64 phys, u64 virt, u64 flags);
|
||||||
|
|
||||||
|
|||||||
@@ -9,5 +9,6 @@ void cmd_meow();
|
|||||||
void cmd_help();
|
void cmd_help();
|
||||||
void cmd_regs();
|
void cmd_regs();
|
||||||
void print_regs();
|
void print_regs();
|
||||||
|
int rectest(int a);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -5,6 +5,6 @@
|
|||||||
|
|
||||||
#include <drivers/shitgui.h>
|
#include <drivers/shitgui.h>
|
||||||
|
|
||||||
void ksh(SG_Context* sg_ctx);
|
void ksh();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+14
-7
@@ -3,35 +3,42 @@
|
|||||||
|
|
||||||
ENTRY(_start)
|
ENTRY(_start)
|
||||||
|
|
||||||
|
KERNEL_VMA_OFFSET = 0xFFFFFFFF80000000;
|
||||||
|
KERNEL_PHYS_START = 0x00100000;
|
||||||
|
|
||||||
SECTIONS
|
SECTIONS
|
||||||
{
|
{
|
||||||
. = 0x100000;
|
. = KERNEL_VMA_OFFSET + KERNEL_PHYS_START;
|
||||||
_kernel_start = .;
|
|
||||||
|
|
||||||
.text : {
|
_kernel_start = .;
|
||||||
|
_kernel_phys_start = . - KERNEL_VMA_OFFSET;
|
||||||
|
|
||||||
|
.text : AT(ADDR(.text) - KERNEL_VMA_OFFSET) {
|
||||||
*(.text.entry)
|
*(.text.entry)
|
||||||
*(.text*)
|
*(.text*)
|
||||||
}
|
}
|
||||||
|
|
||||||
.rodata : {
|
.rodata : AT(ADDR(.rodata) - KERNEL_VMA_OFFSET) {
|
||||||
*(.rodata*)
|
*(.rodata*)
|
||||||
}
|
}
|
||||||
|
|
||||||
.data : {
|
.data : AT(ADDR(.data) - KERNEL_VMA_OFFSET) {
|
||||||
*(.data*)
|
*(.data*)
|
||||||
}
|
}
|
||||||
|
|
||||||
.bss : {
|
.bss : AT(ADDR(.bss) - KERNEL_VMA_OFFSET) {
|
||||||
*(.bss*)
|
*(.bss*)
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
}
|
}
|
||||||
|
|
||||||
.stack (ALIGN(16)) : {
|
.stack (ALIGN(16)) : AT(ADDR(.stack) - KERNEL_VMA_OFFSET) {
|
||||||
*(.stack)
|
*(.stack)
|
||||||
}
|
}
|
||||||
|
|
||||||
. = ALIGN(4096);
|
. = ALIGN(4096);
|
||||||
|
|
||||||
_kernel_end = .;
|
_kernel_end = .;
|
||||||
|
_kernel_phys_end = . - KERNEL_VMA_OFFSET;
|
||||||
|
|
||||||
/DISCARD/ : {
|
/DISCARD/ : {
|
||||||
*(.note*)
|
*(.note*)
|
||||||
|
|||||||
@@ -3,15 +3,30 @@
|
|||||||
|
|
||||||
bits 64
|
bits 64
|
||||||
|
|
||||||
|
%define KERNEL_VMA 0xFFFFFFFF80000000
|
||||||
|
%define PAGE_PRESENT (1 << 0)
|
||||||
|
%define PAGE_RW (1 << 1)
|
||||||
|
%define PAGE_HUGE (1 << 7)
|
||||||
|
|
||||||
section .bss
|
section .bss
|
||||||
align 4096
|
align 4096
|
||||||
guard_page:
|
pml4_table:
|
||||||
|
resb 4096
|
||||||
|
pdpt_table:
|
||||||
|
resb 4096
|
||||||
|
pd_table:
|
||||||
resb 4096
|
resb 4096
|
||||||
|
|
||||||
section .stack nobits
|
section .stack nobits
|
||||||
align 16
|
align 4096
|
||||||
|
|
||||||
|
global stack_guard
|
||||||
|
stack_guard:
|
||||||
|
resb 4096
|
||||||
|
|
||||||
|
global stack_bottom
|
||||||
stack_bottom:
|
stack_bottom:
|
||||||
resb 16384
|
resb 16384
|
||||||
stack_top:
|
stack_top:
|
||||||
|
|
||||||
section .text
|
section .text
|
||||||
@@ -22,26 +37,72 @@ extern kmain
|
|||||||
_start:
|
_start:
|
||||||
cli
|
cli
|
||||||
|
|
||||||
|
mov r12, rdi
|
||||||
|
|
||||||
mov rsp, stack_top
|
mov rsp, stack_top
|
||||||
|
mov rax, KERNEL_VMA
|
||||||
|
sub rsp, rax
|
||||||
|
|
||||||
|
mov rdi, pml4_table
|
||||||
|
sub rdi, KERNEL_VMA
|
||||||
|
|
||||||
|
mov rsi, pdpt_table
|
||||||
|
sub rsi, KERNEL_VMA
|
||||||
|
|
||||||
|
mov rdx, pd_table
|
||||||
|
sub rdx, KERNEL_VMA
|
||||||
|
|
||||||
|
mov rax, rsi
|
||||||
|
or rax, PAGE_PRESENT | PAGE_RW
|
||||||
|
mov [rdi], rax
|
||||||
|
|
||||||
|
mov [rdi + 4088], rax
|
||||||
|
|
||||||
|
mov rax, rdx
|
||||||
|
or rax, PAGE_PRESENT | PAGE_RW
|
||||||
|
mov [rsi], rax
|
||||||
|
|
||||||
|
mov [rsi + 4080], rax
|
||||||
|
|
||||||
|
mov rcx, 0
|
||||||
|
mov rbx, 512
|
||||||
|
mov rax, rdx
|
||||||
|
|
||||||
|
.fill_pd_loop:
|
||||||
|
mov r8, rcx
|
||||||
|
or r8, PAGE_PRESENT | PAGE_RW | PAGE_HUGE
|
||||||
|
mov [rax], r8
|
||||||
|
|
||||||
|
add rcx, 0x200000
|
||||||
|
add rax, 8
|
||||||
|
dec rbx
|
||||||
|
jnz .fill_pd_loop
|
||||||
|
mov cr3, rdi
|
||||||
|
|
||||||
|
mov rax, .higher_half
|
||||||
|
jmp rax
|
||||||
|
|
||||||
|
.higher_half:
|
||||||
|
mov rsp, stack_top
|
||||||
|
mov rdi, r12
|
||||||
xor rbp, rbp
|
xor rbp, rbp
|
||||||
|
|
||||||
call kmain
|
call kmain
|
||||||
|
|
||||||
.hang:
|
.hang:
|
||||||
|
cli
|
||||||
hlt
|
hlt
|
||||||
jmp .hang
|
jmp .hang
|
||||||
|
|
||||||
|
|
||||||
gdt_flush:
|
gdt_flush:
|
||||||
lgdt [rdi]
|
lgdt [rdi]
|
||||||
|
|
||||||
mov ax, 0x10
|
mov ax, 0x10
|
||||||
mov ds, ax
|
mov ds, ax
|
||||||
mov es, ax
|
mov es, ax
|
||||||
mov fs, ax
|
mov fs, ax
|
||||||
mov gs, ax
|
mov gs, ax
|
||||||
mov ss, ax
|
mov ss, ax
|
||||||
|
|
||||||
pop rdi
|
pop rdi
|
||||||
push 0x08
|
push 0x08
|
||||||
push rdi
|
push rdi
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ void isr_handler_c(Registers *regs) {
|
|||||||
void irq_handler_c(Registers *regs) {
|
void irq_handler_c(Registers *regs) {
|
||||||
switch (regs->int_no) {
|
switch (regs->int_no) {
|
||||||
case 32: return outb(MASTER_COMMAND, 0x20);
|
case 32: return outb(MASTER_COMMAND, 0x20);
|
||||||
case 33: return kb_handler(regs);
|
case 33: return kb_handler();
|
||||||
default: outb(SLAVE_COMMAND, 0x20); break;
|
default: outb(SLAVE_COMMAND, 0x20); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -112,16 +112,16 @@ void draw_panic_bg() {
|
|||||||
u8 rand_num = shitrand() % msg_count;
|
u8 rand_num = shitrand() % msg_count;
|
||||||
|
|
||||||
kprintf("\n\n");
|
kprintf("\n\n");
|
||||||
kprintf("\t\t\t\t^tKERNEL PANIC^! :( \n");
|
kprintf("\t\t\t\t^bKERNEL PANIC^! :( \n");
|
||||||
kprintf("\t\t--------------------------------\n");
|
kprintf("\t\t--------------------------------\n");
|
||||||
kprintf("\t\t\t\t^r%s^!\n", fun_messages[rand_num]);
|
kprintf("\t\t^y%s^!\n", fun_messages[rand_num]);
|
||||||
kprintf("\t\t--------------------------------\n");
|
kprintf("\t\t--------------------------------\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((noreturn)) void panic_exception(Registers *regs) {
|
__attribute__((noreturn)) void panic_exception(Registers *regs) {
|
||||||
draw_panic_bg();
|
draw_panic_bg();
|
||||||
|
|
||||||
kprintf("\t\t^yCPU EXCEPTION^!: ^m%s^! (%d)\n", exception_messages[regs->int_no], regs->int_no);
|
kprintf("\t\t^yCPU EXCEPTION^!: ^b%s^! (%d)\n", exception_messages[regs->int_no], regs->int_no);
|
||||||
if (regs->err_code) kprintf("\t\t^yError Code^!: %X\n", regs->err_code);
|
if (regs->err_code) kprintf("\t\t^yError Code^!: %X\n", regs->err_code);
|
||||||
kprintf("\t\t^yInstruction Pointer^! (^yRIP^!): %X\n", regs->rip);
|
kprintf("\t\t^yInstruction Pointer^! (^yRIP^!): %X\n", regs->rip);
|
||||||
kprintf("\t\t^yCode Segment^! (^yCS^!): %X\n", regs->cs);
|
kprintf("\t\t^yCode Segment^! (^yCS^!): %X\n", regs->cs);
|
||||||
@@ -133,7 +133,7 @@ __attribute__((noreturn)) void panic_exception(Registers *regs) {
|
|||||||
kprintf("\t\t^yFaulting Address^! (^yCR2^!): %X\n", cr2);
|
kprintf("\t\t^yFaulting Address^! (^yCR2^!): %X\n", cr2);
|
||||||
}
|
}
|
||||||
kprintf("\t\t--------------------------------\n");
|
kprintf("\t\t--------------------------------\n");
|
||||||
kprintf("\t\t\t\t^yREGSISTERS^!\n");
|
kprintf("\t\t\t\t^yREGISTERS^!\n");
|
||||||
kprintf("\t\t--------------------------------\n");
|
kprintf("\t\t--------------------------------\n");
|
||||||
kprintf("\t\t^yRAX^!=%X, ^yRBX^!=%X\n", regs->rax, regs->rbx);
|
kprintf("\t\t^yRAX^!=%X, ^yRBX^!=%X\n", regs->rax, regs->rbx);
|
||||||
kprintf("\t\t^yRCX^!=%X, ^yRDX^!=%X\n", regs->rcx, regs->rdx);
|
kprintf("\t\t^yRCX^!=%X, ^yRDX^!=%X\n", regs->rcx, regs->rdx);
|
||||||
@@ -144,7 +144,7 @@ __attribute__((noreturn)) void panic_exception(Registers *regs) {
|
|||||||
kprintf("\t\t^yR13^!=%X, ^yR14^!=%X\n", regs->r13, regs->r14);
|
kprintf("\t\t^yR13^!=%X, ^yR14^!=%X\n", regs->r13, regs->r14);
|
||||||
kprintf("\t\t^yR15^!=%X\n",regs->r15);
|
kprintf("\t\t^yR15^!=%X\n",regs->r15);
|
||||||
kprintf("\t\t--------------------------------\n");
|
kprintf("\t\t--------------------------------\n");
|
||||||
kprintf("\t\t\t\t^tSystem halted.^!\n");
|
kprintf("\t\t\t\t^bSystem halted.^!\n");
|
||||||
|
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
@@ -154,7 +154,7 @@ __attribute__((noreturn)) void panic(const char* msg) {
|
|||||||
|
|
||||||
kprintf("\t\t^yReason^!: %s\n", msg);
|
kprintf("\t\t^yReason^!: %s\n", msg);
|
||||||
kprintf("\t\t--------------------------------\n");
|
kprintf("\t\t--------------------------------\n");
|
||||||
kprintf("\t\t\t\t^tSystem halted.^!\n");
|
kprintf("\t\t\t\t^bSystem halted.^!\n");
|
||||||
|
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
@@ -96,6 +96,9 @@ static void console_putc(char c) {
|
|||||||
s_cursor_pos.x = 0;
|
s_cursor_pos.x = 0;
|
||||||
s_cursor_pos.y += s_font.h;
|
s_cursor_pos.y += s_font.h;
|
||||||
}
|
}
|
||||||
|
if (s_cursor_pos.y >= ctx_ptr->height) {
|
||||||
|
console_clear(s_bg_color); // TODO: do scrolling
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void kprint(const char *str) {
|
void kprint(const char *str) {
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ unsigned char keyboard_map_shifted[128] = {
|
|||||||
bool shift_pressed = false;
|
bool shift_pressed = false;
|
||||||
kb_buffer kb_buf = {0};
|
kb_buffer kb_buf = {0};
|
||||||
|
|
||||||
void kb_handler(Registers *regs) {
|
void kb_handler() {
|
||||||
u16 next_head = (kb_buf.head + 1) % KB_BUFF_SIZE;
|
u16 next_head = (kb_buf.head + 1) % KB_BUFF_SIZE;
|
||||||
|
|
||||||
u8 scancode = inb(0x60);
|
u8 scancode = inb(0x60);
|
||||||
@@ -112,5 +112,4 @@ void kb_handler(Registers *regs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
outb(MASTER_COMMAND, 0x20);
|
outb(MASTER_COMMAND, 0x20);
|
||||||
// kprintf("Key: %x; shift is %spressed\n", scancode, shift_pressed ? "" : "not ");
|
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-11
@@ -23,32 +23,41 @@
|
|||||||
#define BG_COLOR 0x111111
|
#define BG_COLOR 0x111111
|
||||||
|
|
||||||
extern u64 _kernel_end;
|
extern u64 _kernel_end;
|
||||||
|
static SG_Context sg_ctx;
|
||||||
|
|
||||||
void kmain(Bootinfo* info) {
|
void kmain(Bootinfo* info) {
|
||||||
u32 *fb = (u32*)info->framebuffer.base;
|
serial_init();
|
||||||
if (!fb) return;
|
serial_write("Kernel started\n");
|
||||||
|
|
||||||
|
gdt_init();
|
||||||
|
serial_write("GDT initialized\n");
|
||||||
|
idt_init();
|
||||||
|
serial_write("IDT initialized\n");
|
||||||
|
pic_remap();
|
||||||
|
serial_write("PIC remapped\n");
|
||||||
|
pmm_init(&info->mem);
|
||||||
|
serial_write("PMM initialized\n");
|
||||||
|
vmm_init(info);
|
||||||
|
serial_write("VMM initialized\n");
|
||||||
|
|
||||||
|
info = (Bootinfo*)PHYS_TO_HHDM((u64)info);
|
||||||
|
|
||||||
|
u32 *fb = (u32*)info->framebuffer.base;
|
||||||
|
if (!fb) return serial_write("No framebuffer found!!");
|
||||||
|
|
||||||
SG_Context sg_ctx = {0};
|
|
||||||
sg_ctx.fb = fb;
|
sg_ctx.fb = fb;
|
||||||
sg_ctx.height = info->framebuffer.height;
|
sg_ctx.height = info->framebuffer.height;
|
||||||
sg_ctx.width = info->framebuffer.width;
|
sg_ctx.width = info->framebuffer.width;
|
||||||
sg_ctx.pitch = info->framebuffer.pitch;
|
sg_ctx.pitch = info->framebuffer.pitch;
|
||||||
|
|
||||||
serial_init();
|
|
||||||
console_init(&sg_ctx);
|
console_init(&sg_ctx);
|
||||||
console_clear(BG_COLOR);
|
console_clear(BG_COLOR);
|
||||||
console_set_color(FG_COLOR);
|
console_set_color(FG_COLOR);
|
||||||
console_set_default_color(FG_COLOR);
|
console_set_default_color(FG_COLOR);
|
||||||
|
|
||||||
gdt_init();
|
|
||||||
idt_init();
|
|
||||||
pic_remap();
|
|
||||||
pmm_init(info->mem);
|
|
||||||
vmm_init(info);
|
|
||||||
|
|
||||||
show_splash(&sg_ctx);
|
show_splash(&sg_ctx);
|
||||||
|
|
||||||
ksh(&sg_ctx);
|
ksh();
|
||||||
|
|
||||||
panic("Kernel main loop exited");
|
panic("Kernel main loop exited");
|
||||||
}
|
}
|
||||||
+20
-7
@@ -9,29 +9,42 @@
|
|||||||
#include <types.h>
|
#include <types.h>
|
||||||
|
|
||||||
#include "bootinfo.h"
|
#include "bootinfo.h"
|
||||||
|
#include "mm/vmm.h"
|
||||||
|
|
||||||
extern u64 _kernel_start;
|
extern u64 _kernel_start;
|
||||||
extern u64 _kernel_end;
|
extern u64 _kernel_end;
|
||||||
|
|
||||||
u8* bitmap = nullptr;
|
u8* bitmap = nullptr;
|
||||||
u64 bitmap_size_g = 0;
|
u64 bitmap_size_g = 0;
|
||||||
|
static u64 total_mem_size = 0;
|
||||||
|
|
||||||
void pmm_init(BI_MemoryMap mmap) {
|
u64 pmm_get_total_mem() {
|
||||||
u64 descriptors_count = mmap.map_size / mmap.descriptor_size;
|
return total_mem_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm_init(BI_MemoryMap* mmap) {
|
||||||
|
u64 descriptors_count = mmap->map_size / mmap->descriptor_size;
|
||||||
u64 max_physical_address = 0;
|
u64 max_physical_address = 0;
|
||||||
|
|
||||||
for (u64 i = 0; i < descriptors_count; i++) {
|
for (u64 i = 0; i < descriptors_count; i++) {
|
||||||
efi_memory_descriptor_k* descriptor = (efi_memory_descriptor_k*)((u8*)mmap.map + (i * mmap.descriptor_size));
|
efi_memory_descriptor_k* descriptor = (efi_memory_descriptor_k*)((u8*)mmap->map + (i * mmap->descriptor_size));
|
||||||
|
if (descriptor->type == EfiMemoryMappedIO ||
|
||||||
|
descriptor->type == EfiMemoryMappedIOPortSpace ||
|
||||||
|
descriptor->type == EfiUnusableMemory ||
|
||||||
|
descriptor->type == EfiReservedMemoryType ||
|
||||||
|
descriptor->type == EfiPalCode) continue;
|
||||||
|
|
||||||
u64 nominee = descriptor->physical_start + descriptor->number_of_pages * PAGE_SIZE;
|
u64 nominee = descriptor->physical_start + descriptor->number_of_pages * PAGE_SIZE;
|
||||||
max_physical_address = MAX(nominee, max_physical_address);
|
max_physical_address = MAX(nominee, max_physical_address);
|
||||||
}
|
}
|
||||||
|
total_mem_size = max_physical_address;
|
||||||
|
|
||||||
u64 pages_count = max_physical_address / PAGE_SIZE;
|
u64 pages_count = max_physical_address / PAGE_SIZE;
|
||||||
u64 bitmap_size = (pages_count + 7) / 8;
|
u64 bitmap_size = (pages_count + 7) / 8;
|
||||||
efi_memory_descriptor_k* desc_to_save = nullptr;
|
efi_memory_descriptor_k* desc_to_save = nullptr;
|
||||||
|
|
||||||
for (u64 i = 0; i < descriptors_count; i++) {
|
for (u64 i = 0; i < descriptors_count; i++) {
|
||||||
efi_memory_descriptor_k* descriptor = (efi_memory_descriptor_k*)((u8*)mmap.map + (i * mmap.descriptor_size));
|
efi_memory_descriptor_k* descriptor = (efi_memory_descriptor_k*)((u8*)mmap->map + (i * mmap->descriptor_size));
|
||||||
// scary
|
// scary
|
||||||
if ((descriptor->type == EfiConventionalMemory) && \
|
if ((descriptor->type == EfiConventionalMemory) && \
|
||||||
((descriptor->number_of_pages * PAGE_SIZE) >= bitmap_size) && \
|
((descriptor->number_of_pages * PAGE_SIZE) >= bitmap_size) && \
|
||||||
@@ -51,15 +64,15 @@ void pmm_init(BI_MemoryMap mmap) {
|
|||||||
memset(bitmap, 0xFF, bitmap_size);
|
memset(bitmap, 0xFF, bitmap_size);
|
||||||
|
|
||||||
for (u64 i = 0; i < descriptors_count; i++) {
|
for (u64 i = 0; i < descriptors_count; i++) {
|
||||||
efi_memory_descriptor_k* descriptor = (efi_memory_descriptor_k*)((u8*)mmap.map + (i * mmap.descriptor_size));// this shit will haunt my dreams
|
efi_memory_descriptor_k* descriptor = (efi_memory_descriptor_k*)((u8*)mmap->map + (i * mmap->descriptor_size));// this shit will haunt my dreams
|
||||||
if (descriptor->type != EfiConventionalMemory) continue;
|
if (descriptor->type != EfiConventionalMemory) continue;
|
||||||
u64 start_addr = descriptor->physical_start;
|
u64 start_addr = descriptor->physical_start;
|
||||||
u64 end_addr = start_addr + (descriptor->number_of_pages * PAGE_SIZE);
|
u64 end_addr = start_addr + (descriptor->number_of_pages * PAGE_SIZE);
|
||||||
for (u64 j = start_addr; j < end_addr; j += PAGE_SIZE) BITMAP_UNSET(bitmap, j);
|
for (u64 j = start_addr; j < end_addr; j += PAGE_SIZE) BITMAP_UNSET(bitmap, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 k_start = (u64)&_kernel_start;
|
u64 k_start = KERNEL_VIRT_TO_PHYS((u64)&_kernel_start);
|
||||||
u64 k_end = (u64)&_kernel_end;
|
u64 k_end = KERNEL_VIRT_TO_PHYS((u64)&_kernel_end);
|
||||||
|
|
||||||
u64 bitmap_start = (u64)bitmap;
|
u64 bitmap_start = (u64)bitmap;
|
||||||
u64 bitmap_end = bitmap_start + bitmap_size_g;
|
u64 bitmap_end = bitmap_start + bitmap_size_g;
|
||||||
|
|||||||
+56
-34
@@ -11,6 +11,7 @@
|
|||||||
#include "bootinfo.h"
|
#include "bootinfo.h"
|
||||||
|
|
||||||
u64* pml4_kernel = nullptr;
|
u64* pml4_kernel = nullptr;
|
||||||
|
static bool is_initialized = false;
|
||||||
|
|
||||||
extern u64 _kernel_start;
|
extern u64 _kernel_start;
|
||||||
extern u64 _kernel_end;
|
extern u64 _kernel_end;
|
||||||
@@ -20,6 +21,12 @@ extern GDTDescriptor gdt[];
|
|||||||
extern TSS tss[];
|
extern TSS tss[];
|
||||||
extern IDTEntry idt[];
|
extern IDTEntry idt[];
|
||||||
extern u8 double_fault_stack[];
|
extern u8 double_fault_stack[];
|
||||||
|
extern u8 stack_guard;
|
||||||
|
|
||||||
|
static u64* get_table_virt(u64 phys) {
|
||||||
|
if (is_initialized) return (u64*)PHYS_TO_HHDM(phys);
|
||||||
|
return (u64*)phys;
|
||||||
|
}
|
||||||
|
|
||||||
void vmm_map_page(u64* pml4, u64 phys, u64 virt, u64 flags) {
|
void vmm_map_page(u64* pml4, u64 phys, u64 virt, u64 flags) {
|
||||||
u64 pt_idx = VMM_PT_INDEX(virt);
|
u64 pt_idx = VMM_PT_INDEX(virt);
|
||||||
@@ -29,26 +36,54 @@ void vmm_map_page(u64* pml4, u64 phys, u64 virt, u64 flags) {
|
|||||||
|
|
||||||
if (!(pml4[pml4_idx] & PTE_PRESENT)) {
|
if (!(pml4[pml4_idx] & PTE_PRESENT)) {
|
||||||
u64* addr = pmm_alloc_page();
|
u64* addr = pmm_alloc_page();
|
||||||
memset(addr, 0, PAGE_SIZE);
|
u64* addr_virt = get_table_virt((u64)addr);
|
||||||
|
memset(addr_virt, 0, PAGE_SIZE);
|
||||||
pml4[pml4_idx] = (u64)addr | PTE_PRESENT | PTE_RW;
|
pml4[pml4_idx] = (u64)addr | PTE_PRESENT | PTE_RW;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64* pdpt = (u64*)PTE_GET_ADDR(pml4[pml4_idx]);
|
u64* pdpt = (u64*)PTE_GET_ADDR(pml4[pml4_idx]);
|
||||||
if (!(pdpt[pdpt_idx] & PTE_PRESENT)) {
|
u64* pdpt_virt = get_table_virt((u64)pdpt);
|
||||||
|
|
||||||
|
if (!(pdpt_virt[pdpt_idx] & PTE_PRESENT)) {
|
||||||
u64* addr = pmm_alloc_page();
|
u64* addr = pmm_alloc_page();
|
||||||
memset(addr, 0, PAGE_SIZE);
|
u64* addr_virt = get_table_virt((u64)addr);
|
||||||
pdpt[pdpt_idx] = (u64)addr | PTE_PRESENT | PTE_RW;
|
memset(addr_virt, 0, PAGE_SIZE);
|
||||||
|
pdpt_virt[pdpt_idx] = (u64)addr | PTE_PRESENT | PTE_RW;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64* pd = (u64*)PTE_GET_ADDR(pdpt[pdpt_idx]);
|
u64* pd = (u64*)PTE_GET_ADDR(pdpt_virt[pdpt_idx]);
|
||||||
if (!(pd[pd_idx] & PTE_PRESENT)) {
|
u64* pd_virt = get_table_virt((u64)pd);
|
||||||
|
|
||||||
|
if (!(pd_virt[pd_idx] & PTE_PRESENT)) {
|
||||||
u64* addr = pmm_alloc_page();
|
u64* addr = pmm_alloc_page();
|
||||||
memset(addr, 0, PAGE_SIZE);
|
u64* addr_virt = get_table_virt((u64)addr);
|
||||||
pd[pd_idx] = (u64)addr | PTE_PRESENT | PTE_RW;
|
memset(addr_virt, 0, PAGE_SIZE);
|
||||||
|
pd_virt[pd_idx] = (u64)addr | PTE_PRESENT | PTE_RW;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64* pt = (u64*)PTE_GET_ADDR(pd[pd_idx]);
|
u64* pt = (u64*)PTE_GET_ADDR(pd_virt[pd_idx]);
|
||||||
pt[pt_idx] = phys | flags;
|
u64* pt_virt = get_table_virt((u64)pt);
|
||||||
|
pt_virt[pt_idx] = phys | flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
void vmm_unmap_page(u64* pml4, u64 virt) {
|
||||||
|
u64 pt_idx = VMM_PT_INDEX(virt);
|
||||||
|
u64 pd_idx = VMM_PD_INDEX(virt);
|
||||||
|
u64 pdpt_idx = VMM_PDPT_INDEX(virt);
|
||||||
|
u64 pml4_idx = VMM_PML4_INDEX(virt);
|
||||||
|
|
||||||
|
if (!(pml4[pml4_idx] & PTE_PRESENT)) return;
|
||||||
|
u64* pdpt = get_table_virt(PTE_GET_ADDR(pml4[pml4_idx]));
|
||||||
|
|
||||||
|
if (!(pdpt[pdpt_idx] & PTE_PRESENT)) return;
|
||||||
|
u64* pd = get_table_virt(PTE_GET_ADDR(pdpt[pdpt_idx]));
|
||||||
|
|
||||||
|
if (!(pd[pd_idx] & PTE_PRESENT)) return;
|
||||||
|
u64* pt = get_table_virt(PTE_GET_ADDR(pd[pd_idx]));
|
||||||
|
|
||||||
|
pt[pt_idx] = 0;
|
||||||
|
|
||||||
|
__asm__ volatile("invlpg (%0)" :: "r" (virt) : "memory");
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void load_cr3(u64 pml4_addr) {
|
static inline void load_cr3(u64 pml4_addr) {
|
||||||
@@ -59,34 +94,21 @@ void vmm_init(Bootinfo* info) {
|
|||||||
pml4_kernel = pmm_alloc_page();
|
pml4_kernel = pmm_alloc_page();
|
||||||
memset(pml4_kernel, 0, PAGE_SIZE);
|
memset(pml4_kernel, 0, PAGE_SIZE);
|
||||||
|
|
||||||
u64 k_start = (u64)&_kernel_start;
|
u64 k_virt_start = (u64)&_kernel_start;
|
||||||
u64 k_end = (u64)&_kernel_end;
|
u64 k_virt_end = (u64)&_kernel_end;
|
||||||
|
|
||||||
u64 bitmap_start = (u64)bitmap;
|
|
||||||
u64 bitmap_end = bitmap_start + bitmap_size_g;
|
|
||||||
|
|
||||||
u64 bi_addr = (u64)info;
|
|
||||||
|
|
||||||
u64 fb_start = (u64)info->framebuffer.base;
|
u64 fb_start = (u64)info->framebuffer.base;
|
||||||
u64 fb_end = fb_start + info->framebuffer.base_size;
|
u64 fb_end = fb_start + info->framebuffer.base_size;
|
||||||
|
u64 fb_size = fb_end - fb_start;
|
||||||
|
|
||||||
u64 gdt_addr = (u64)&gdt;
|
u64 max_mem = pmm_get_total_mem();
|
||||||
u64 idt_addr = (u64)&idt;
|
for (u64 i = 0; i < max_mem; i += PAGE_SIZE) vmm_map_page(pml4_kernel, i, PHYS_TO_HHDM(i), PTE_PRESENT | PTE_RW);
|
||||||
u64 tss_addr = (u64)&tss;
|
for (u64 i = k_virt_start; i < k_virt_end; i += PAGE_SIZE) vmm_map_page(pml4_kernel, KERNEL_VIRT_TO_PHYS(i), i, PTE_PRESENT | PTE_RW);
|
||||||
u64 df_stack_addr = (u64)double_fault_stack;
|
for (u64 i = 0; i < fb_size; i += PAGE_SIZE) vmm_map_page(pml4_kernel, fb_start + i, FB_VIRT_BASE + i, PTE_PRESENT | PTE_RW);
|
||||||
|
vmm_unmap_page(pml4_kernel, (u64)&stack_guard);
|
||||||
for (u64 i = k_start; i < k_end; i += PAGE_SIZE) vmm_map_page(pml4_kernel, i, i, PTE_PRESENT | PTE_RW);
|
|
||||||
for (u64 i = bitmap_start; i < bitmap_end; i += PAGE_SIZE) vmm_map_page(pml4_kernel, i, i, PTE_PRESENT | PTE_RW);
|
|
||||||
for (u64 i = fb_start; i < fb_end; i += PAGE_SIZE) vmm_map_page(pml4_kernel, i, i, PTE_PRESENT | PTE_RW);
|
|
||||||
for (u64 i = 0; i < SAFE_SPACE_START_ADDR; i += PAGE_SIZE) vmm_map_page(pml4_kernel, i, i, PTE_PRESENT | PTE_RW);
|
|
||||||
|
|
||||||
vmm_map_page(pml4_kernel, bi_addr, bi_addr, PTE_PRESENT | PTE_RW);
|
|
||||||
vmm_map_page(pml4_kernel, gdt_addr, gdt_addr, PTE_PRESENT | PTE_RW);
|
|
||||||
vmm_map_page(pml4_kernel, idt_addr, idt_addr, PTE_PRESENT | PTE_RW);
|
|
||||||
vmm_map_page(pml4_kernel, tss_addr, tss_addr, PTE_PRESENT | PTE_RW);
|
|
||||||
|
|
||||||
vmm_map_page(pml4_kernel, df_stack_addr, df_stack_addr, PTE_PRESENT | PTE_RW);
|
|
||||||
vmm_map_page(pml4_kernel, df_stack_addr + 4096, df_stack_addr + 4096, PTE_PRESENT | PTE_RW);
|
|
||||||
|
|
||||||
|
bitmap = (u8*)PHYS_TO_HHDM((u64)bitmap);
|
||||||
|
info->framebuffer.base = (u32*)FB_VIRT_BASE;
|
||||||
load_cr3((u64)pml4_kernel);
|
load_cr3((u64)pml4_kernel);
|
||||||
|
is_initialized = true;
|
||||||
}
|
}
|
||||||
@@ -18,17 +18,22 @@ const char* ascii_logo[] = {
|
|||||||
" |_______| "
|
" |_______| "
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int rectest(int a) {
|
||||||
|
volatile int b = a + 1;
|
||||||
|
kprintf("%d", b);
|
||||||
|
return rectest(b * 2);
|
||||||
|
}
|
||||||
|
|
||||||
void cmd_kfetch() {
|
void cmd_kfetch() {
|
||||||
kprintf("\n\n");
|
kprintf("\n\n");
|
||||||
kprintf("^p %s ^0\t\t^g kernel^0@^gtermos\n^0", ascii_logo[0]);
|
kprintf("^p %s ^!\t\t^g kernel^!@^gtermos\n^0", ascii_logo[0]);
|
||||||
kprintf("^p %s ^0\t\t^0-------------\n^0", ascii_logo[1]);
|
kprintf("^p %s ^!\t\t^!-------------\n^!", ascii_logo[1]);
|
||||||
kprintf("^p %s ^0\t\t^gOS^0: termOS 0.0.1\n^0", ascii_logo[2]);
|
kprintf("^p %s ^!\t\t^gOS^!: termOS 0.0.1\n^!", ascii_logo[2]);
|
||||||
kprintf("^p %s ^0\t\t^gKernel^0: sucks\n^0", ascii_logo[3]);
|
kprintf("^p %s ^!\t\t^gKernel^!: sucks\n^!", ascii_logo[3]);
|
||||||
kprintf("^p %s ^0\t\t^gUptime^0: 0h 0m\n^0", ascii_logo[4]);
|
kprintf("^p %s ^!\t\t^gUptime^!: 0h 0m\n^!", ascii_logo[4]);
|
||||||
kprintf("^p %s ^0\t\t^gShell^0: termosh\n^0", ascii_logo[5]);
|
kprintf("^p %s ^!\t\t^gShell^!: termosh\n^!", ascii_logo[5]);
|
||||||
kprintf("^p %s ^p\t\t^gDE^0: shitgui\n^0", ascii_logo[6]);
|
kprintf("^p %s ^p\t\t^gDE^!: shitgui\n^!", ascii_logo[6]);
|
||||||
kprintf("^p %s ^p\t\t^gCPU^0: %s\n^0", ascii_logo[7], "cool one");
|
kprintf("^p %s ^p\t\t^gCPU^!: %s\n^!", ascii_logo[7], "cool one");
|
||||||
kprintf("\n\n");
|
kprintf("\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ typedef enum {
|
|||||||
TOKEN_MEOW,
|
TOKEN_MEOW,
|
||||||
|
|
||||||
TOKEN_REGS,
|
TOKEN_REGS,
|
||||||
|
TOKEN_RECTEST,
|
||||||
TOKEN_PANIC,
|
TOKEN_PANIC,
|
||||||
TOKEN_PANIC_UD2,
|
TOKEN_PANIC_UD2,
|
||||||
TOKEN_PANIC_PF,
|
TOKEN_PANIC_PF,
|
||||||
@@ -55,6 +56,7 @@ static const ksh_command_map token_map[] = {
|
|||||||
{"neofetch", TOKEN_KFETCH},
|
{"neofetch", TOKEN_KFETCH},
|
||||||
|
|
||||||
{"regs", TOKEN_REGS},
|
{"regs", TOKEN_REGS},
|
||||||
|
{"rectest", TOKEN_RECTEST},
|
||||||
{"panic", TOKEN_PANIC},
|
{"panic", TOKEN_PANIC},
|
||||||
{"ud2", TOKEN_PANIC_UD2},
|
{"ud2", TOKEN_PANIC_UD2},
|
||||||
{"pf", TOKEN_PANIC_PF},
|
{"pf", TOKEN_PANIC_PF},
|
||||||
@@ -70,7 +72,7 @@ ksh_token char2token(char* token) {
|
|||||||
return TOKEN_ILLEGAL;
|
return TOKEN_ILLEGAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ksh(SG_Context* sg_ctx) {
|
void ksh() {
|
||||||
while (true) {
|
while (true) {
|
||||||
kprintf("ksh_> ");
|
kprintf("ksh_> ");
|
||||||
char cmdbuff[256];
|
char cmdbuff[256];
|
||||||
@@ -81,11 +83,12 @@ void ksh(SG_Context* sg_ctx) {
|
|||||||
case TOKEN_QUIT: return; // that'll cause panic lol
|
case TOKEN_QUIT: return; // that'll cause panic lol
|
||||||
|
|
||||||
case TOKEN_REGS: cmd_regs(); break;
|
case TOKEN_REGS: cmd_regs(); break;
|
||||||
|
case TOKEN_RECTEST: rectest(0); break;
|
||||||
case TOKEN_PANIC: panic("Manually initiated panic");
|
case TOKEN_PANIC: panic("Manually initiated panic");
|
||||||
case TOKEN_PANIC_UD2: __asm__ volatile ("ud2");
|
case TOKEN_PANIC_UD2: __asm__ volatile ("ud2");
|
||||||
case TOKEN_PANIC_PF: u64* bad_ptr = (u64*)0xDEADBEEF; *bad_ptr = 666;
|
case TOKEN_PANIC_PF: u64* bad_ptr = (u64*)0xDEADBEEF; *bad_ptr = 666;
|
||||||
|
|
||||||
case TOKEN_SPLASH: show_splash(sg_ctx); break;
|
case TOKEN_SPLASH: show_splash(console_get_context()); break;
|
||||||
case TOKEN_CLEAR: console_clear((u32) console_get_colors() & 0xFFFFFFFF); break;
|
case TOKEN_CLEAR: console_clear((u32) console_get_colors() & 0xFFFFFFFF); break;
|
||||||
case TOKEN_HELP: cmd_help(); break;
|
case TOKEN_HELP: cmd_help(); break;
|
||||||
case TOKEN_KFETCH: cmd_kfetch(); break;
|
case TOKEN_KFETCH: cmd_kfetch(); break;
|
||||||
|
|||||||
Reference in New Issue
Block a user