diff --git a/Makefile b/Makefile index b2ccf57..6c70425 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ -SOURCES = src/mem/paging/pmm.c src/string/string.c src/io/kbd/ps2.c src/io/serial/serial.c src/io/term/printf.c src/io/term/term.c src/idt/idt.c src/mem/gdt/gdt.c src/mem/misc/utils.c src/time/timer.c src/kmain.c +SOURCES = src/mem/paging/paging.c src/mem/paging/pmm.c src/string/string.c src/io/kbd/ps2.c src/io/serial/serial.c src/io/term/printf.c src/io/term/term.c src/idt/idt.c src/mem/gdt/gdt.c src/mem/misc/utils.c src/time/timer.c src/kmain.c build: rm -f *.o - x86_64-elf-gcc -g -c -I src $(SOURCES) -Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fno-stack-protector -fno-stack-check -fno-PIC -ffunction-sections -fdata-sections -mcmodel=kernel + x86_64-elf-gcc -g -c -Isrc $(SOURCES) -Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fno-stack-protector -fno-stack-check -fno-PIC -ffunction-sections -fdata-sections -mcmodel=kernel objcopy -O elf64-x86-64 -B i386 -I binary zap-light16.psf zap-light16.o nasm -f elf64 src/idt/idt.S -o idt_stub.o x86_64-elf-ld -o pepperk -T linker.ld *.o diff --git a/src/idt/idt.c b/src/idt/idt.c index 77fa029..5e31e48 100644 --- a/src/idt/idt.c +++ b/src/idt/idt.c @@ -1,8 +1,8 @@ #include "idt.h" #include #include -#include "../io/serial/serial.h" -#include "../io/kbd/ps2.h" +#include "io/serial/serial.h" +#include "io/kbd/ps2.h" #include struct interrupt_descriptor idt[256]; diff --git a/src/io/kbd/ps2.c b/src/io/kbd/ps2.c index 314d197..d8b787a 100644 --- a/src/io/kbd/ps2.c +++ b/src/io/kbd/ps2.c @@ -1,9 +1,9 @@ // PS/2 Keyboard support -#include "../serial/serial.h" +#include "io/serial/serial.h" #include "ps2.h" #include -#include "../term/term.h" +#include "io/term/term.h" #include // The key status bitfield will be used to see if ALT, CONTROL, or SHIFT is pressed diff --git a/src/kmain.c b/src/kmain.c index 1ea9f90..52c8bdd 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -11,6 +11,7 @@ #include "time/timer.h" #include "io/kbd/ps2.h" #include "mem/paging/pmm.h" +#include "mem/paging/paging.h" // Limine version used __attribute__((used, section(".limine_requests"))) @@ -73,6 +74,7 @@ void kmain() hhdm_display(hhdm_request.response); pmm_init(memmap_request.response, hhdm_request.response); + paging_init(); CLEAR_INTERRUPTS; gdt_init(); diff --git a/src/mem/gdt/gdt.c b/src/mem/gdt/gdt.c index 052f18f..68961b5 100644 --- a/src/mem/gdt/gdt.c +++ b/src/mem/gdt/gdt.c @@ -1,6 +1,6 @@ #include "gdt.h" #include -#include "../../io/serial/serial.h" +#include "io/serial/serial.h" #include // Descriptors are 8-byte wide (64bits) diff --git a/src/mem/misc/utils.c b/src/mem/misc/utils.c index 227dab0..bb5ca8a 100644 --- a/src/mem/misc/utils.c +++ b/src/mem/misc/utils.c @@ -1,8 +1,8 @@ #include #include #include -#include "../../kernel.h" -#include "../../string/string.h" +#include "kernel.h" +#include "string/string.h" // We won't be linked to standard library, but still need the basic mem* functions // so everything goes allright with the compiler diff --git a/src/mem/paging/paging.c b/src/mem/paging/paging.c new file mode 100644 index 0000000..39fdd30 --- /dev/null +++ b/src/mem/paging/paging.c @@ -0,0 +1,13 @@ +#include "paging.h" +#include + +void paging_init() +{ + uint64_t cr3; + asm volatile ("mov %%cr3, %0" : "=r"(cr3)); + + // Root directory address (cr3 without 12 less significant bits) + uint64_t* pml4 = PHYS_TO_VIRT(cr3 & ~0xFFF); + + DEBUG("pml4=0x%p", pml4); +} \ No newline at end of file diff --git a/src/mem/paging/paging.h b/src/mem/paging/paging.h index 33da2aa..6dad8c1 100644 --- a/src/mem/paging/paging.h +++ b/src/mem/paging/paging.h @@ -1,16 +1,29 @@ -#ifndef PAGING_PMM_H -#define PAGING_PMM_H - -/* -We are going to use a bitmap, consisting of an array of uint64_t -to represent pages for the PMM (physical memory manager). - -Bit set (1) = page used -Bit clear (0) = page free - -*/ +#ifndef PAGING_H +#define PAGING_H #define PAGE_SIZE 4096 #define BITS_PER_ROW 64 +#include + +void paging_init(); + +extern uint64_t hhdm_off; + +#define PHYS_TO_VIRT(x) ((void*)((uintptr_t)(x) + hhdm_off)) +#define VIRT_TO_PHYS(x) ((uintptr_t)(x) - hhdm_off) + +// Stole it +#define ALIGN_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1)) +#define ALIGN_DOWN(x, align) ((x) & ~((align) - 1)) + +// Page entry special bits +// Bits set on a parent (directory, table) fall back to their children +#define PTE_PRESENT (1ULL << 0) +#define PTE_WRITE (1ULL << 1) +#define PTE_USER (1ULL << 2) +#define PTE_HUGE (1ULL << 7) +#define PTE_NOEXEC (1ULL << 63) + + #endif \ No newline at end of file diff --git a/src/mem/paging/pmm.c b/src/mem/paging/pmm.c index 3ce044b..df98430 100644 --- a/src/mem/paging/pmm.c +++ b/src/mem/paging/pmm.c @@ -12,7 +12,7 @@ to see which pages are used by kernel/bootloader/mmio/fb etc. #include #include #include -#include "../misc/utils.h" +#include "mem/misc/utils.h" #include "pmm.h" /* @@ -27,42 +27,6 @@ and use this for the bitmap. The reserved memory will be ignored. struct usable_memory* usable_mem; struct limine_memmap_entry* biggest_entry; -/* -uint64_t* bitmap; -uint64_t* usable_memory_begin; - -// Bitmap will be placed at the start of the biggest usable region -// 1 bitmap page = 4096 bytes = 32768 bits -// So 1 bitmap page can track up to 32768 pages of memory -static void pmm_allocate_bitmap(struct limine_hhdm_response* hhdm) -{ - uint64_t pages = biggest_entry->length / PAGE_SIZE; - uint64_t bitmap_items = (pages+BITS_PER_ROW-1)/BITS_PER_ROW; - DEBUG("we need %u pages (bits) that will fit in %u uint64_t", pages, bitmap_items); - - bitmap = (uint64_t*)(biggest_entry->base + hhdm->offset); - DEBUG("bitmap will live at 0x%p", bitmap); - - uint64_t bitmap_bytes = bitmap_items*sizeof(uint64_t); - - // All pages are marked free since we're in a USABLE region - memset(bitmap, 0x00, bitmap_bytes); - - uint64_t bitmap_pages = (bitmap_bytes + PAGE_SIZE - 1) / PAGE_SIZE; - - // The 1st memory page we can use is at bitmap + bitmap_pages - usable_memory_begin = (void*)((uintptr_t)bitmap + bitmap_pages*PAGE_SIZE); - - // Mark bitmap as allocated - for (uint64_t i=0; i -#include "../io/serial/serial.h" +#include "io/serial/serial.h" #include /*