From e728571393efed62ff818a7346db4fb2ab932cc3 Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Thu, 23 May 2024 20:53:15 +0200 Subject: [PATCH] Finished Virtual Memory Manager!!! Paging etc. now works perfectly* *i have no idea if it works right, but it doesn't seem to be broken --- include/interrupt/exceptions.h | 150 ++--------------------- include/interrupt/exceptions.h.bak | 185 +++++++++++++++++++++++++++++ include/memory/addresses.h | 7 ++ include/memory/physical.h | 20 ++-- include/memory/virtual.h | 154 +++++++++++++++++++++++- src/kernel.c | 64 ++++++---- src/stage2.asm | 6 +- todo.txt | 6 +- 8 files changed, 414 insertions(+), 178 deletions(-) create mode 100644 include/interrupt/exceptions.h.bak create mode 100644 include/memory/addresses.h diff --git a/include/interrupt/exceptions.h b/include/interrupt/exceptions.h index 3a118b3..3122ccb 100644 --- a/include/interrupt/exceptions.h +++ b/include/interrupt/exceptions.h @@ -9,146 +9,36 @@ __attribute__ ((interrupt)) void excp_divideErr(intFrame_t* iframe) { // ISR 0 fgColor = RED; print("[ Division Error ]\n"); - __asm__ volatile ("cli; hlt"); // Halt system -} - -__attribute__ ((interrupt)) -void excp_debugErr(intFrame_t* iframe) { // ISR 1 - fgColor = RED; - print("[ Debug Exception ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_nmiInterrupt(intFrame_t* iframe) { // ISR 2 - fgColor = RED; - print("[ Non-maskable Interrupt ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_breakpoint(intFrame_t* iframe) { // ISR 3 - fgColor = RED; - print("[ Breakpoint ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_overflow(intFrame_t* iframe) { // ISR 4 - fgColor = RED; - print("[ Overflow Error ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_boundRangeExceed(intFrame_t* iframe) { // ISR 5 - fgColor = RED; - print("[ Bound Range Exceeded ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_invalidOpcode(intFrame_t* iframe) { // ISR 6 - fgColor = RED; - print("[ Invalid OpCode ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_deviceNA(intFrame_t* iframe) { // ISR 7 - fgColor = RED; - print("[ Device Not Available ]\n"); - __asm__ volatile ("cli; hlt"); + iframe->eip++; } __attribute__ ((interrupt)) void excp_doubleFault(intFrame_t* iframe, uint32_t errCode) { // ISR 8 fgColor = RED; - print("[ Double Fault : 0x"); - print(itoa(errCode, 16)); - print(" ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_coprocOverrun(intFrame_t* iframe) { // ISR 9 - fgColor = RED; - print("[ Coprocessor Segment Overrun ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_invalidTss(intFrame_t* iframe, uint32_t errCode) { // ISR 10 - fgColor = RED; - print("[ Invalid TSS : 0x"); - print(itoa(errCode, 16)); - print(" ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_noSegment(intFrame_t* iframe, uint32_t errCode) { // ISR 11 - fgColor = RED; - print("[ Segment Not Present : 0x"); - print(itoa(errCode, 16)); - print(" ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_stackSegmentFault(intFrame_t* iframe, uint32_t errCode) { // ISR 12 - fgColor = RED; - print("[ Stack-Segment Fault : 0x"); - print(itoa(errCode, 16)); - print(" ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_generalProtectionFault(intFrame_t* iframe, uint32_t errCode) { // ISR 13 - fgColor = RED; - print("[ General Protection Fault : 0x"); - print(itoa(errCode, 16)); - print(" ]\n"); + print("[ Double Fault ]\n"); // Error code should be 0 (TODO: Maybe check if it's != 0?) __asm__ volatile ("cli; hlt"); } __attribute__ ((interrupt)) void excp_pageFault(intFrame_t* iframe, uint32_t errCode) { // ISR 14 + uint32_t badAddress = 0; + + // cr2 should contain invalid address + __asm__ volatile("movl %%cr2, %0" : "=r"(badAddress)); + fgColor = RED; print("[ Page Fault : 0x"); print(itoa(errCode, 16)); - print(" ]\n"); + print(" ] - Tried to access 0x"); + print(itoa(badAddress, 16)); + print(".\n"); __asm__ volatile ("cli; hlt"); } __attribute__ ((interrupt)) void excp_reserved(intFrame_t* iframe) { // ISR 15 fgColor = RED; - print("[ Reserved Exception ]\n"); // Shouldn't get this one - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_floatingPointErr(intFrame_t* iframe) { // ISR 16 - fgColor = RED; - print("[ x87 FPU Floating-Point Error ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_alignCheck(intFrame_t* iframe, uint32_t errCode) { // ISR 17 - fgColor = RED; - print("[ Alignment Check : 0x"); - print(itoa(errCode, 16)); - print(" ]\n"); - __asm__ volatile ("cli; hlt"); -} - -__attribute__ ((interrupt)) -void excp_machineCheck(intFrame_t* iframe) { // ISR 18 - fgColor = RED; - print("[ Machine Check ]\n"); + print("[ Unknown Exception ]\n"); __asm__ volatile ("cli; hlt"); } @@ -157,29 +47,13 @@ void excp_machineCheck(intFrame_t* iframe) { // ISR 18 // Repetative isr definitions void setupExceptions() { for (uint8_t i = 0; i < 32; i++) { - idtSetGate(i, excp_reserved, TRAP_GATE_FLAGS); + idtSetGate(i, excp_reserved, TRAP_GATE_FLAGS); // Doesn't account for err codes, will just halt anyways } idtSetGate(0, excp_divideErr, TRAP_GATE_FLAGS); - idtSetGate(1, excp_debugErr, TRAP_GATE_FLAGS); - idtSetGate(2, excp_nmiInterrupt, TRAP_GATE_FLAGS); - idtSetGate(3, excp_breakpoint, TRAP_GATE_FLAGS); - idtSetGate(4, excp_overflow, TRAP_GATE_FLAGS); - idtSetGate(5, excp_boundRangeExceed, TRAP_GATE_FLAGS); - idtSetGate(6, excp_invalidOpcode, TRAP_GATE_FLAGS); - idtSetGate(7, excp_deviceNA, TRAP_GATE_FLAGS); idtSetGate(8, excp_doubleFault, TRAP_GATE_FLAGS); - idtSetGate(9, excp_coprocOverrun, TRAP_GATE_FLAGS); - idtSetGate(10, excp_invalidTss, TRAP_GATE_FLAGS); - idtSetGate(11, excp_noSegment, TRAP_GATE_FLAGS); - idtSetGate(12, excp_stackSegmentFault, TRAP_GATE_FLAGS); - idtSetGate(13, excp_generalProtectionFault, TRAP_GATE_FLAGS); idtSetGate(14, excp_pageFault, TRAP_GATE_FLAGS); idtSetGate(15, excp_reserved, TRAP_GATE_FLAGS); - idtSetGate(16, excp_floatingPointErr, TRAP_GATE_FLAGS); - idtSetGate(17, excp_alignCheck, TRAP_GATE_FLAGS); - idtSetGate(18, excp_machineCheck, TRAP_GATE_FLAGS); - // Other are reserved } #endif diff --git a/include/interrupt/exceptions.h.bak b/include/interrupt/exceptions.h.bak new file mode 100644 index 0000000..3a118b3 --- /dev/null +++ b/include/interrupt/exceptions.h.bak @@ -0,0 +1,185 @@ +#ifndef ISRS_H +#define ISRS_H + +#include +#include + +// Exception handlers +__attribute__ ((interrupt)) +void excp_divideErr(intFrame_t* iframe) { // ISR 0 + fgColor = RED; + print("[ Division Error ]\n"); + __asm__ volatile ("cli; hlt"); // Halt system +} + +__attribute__ ((interrupt)) +void excp_debugErr(intFrame_t* iframe) { // ISR 1 + fgColor = RED; + print("[ Debug Exception ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_nmiInterrupt(intFrame_t* iframe) { // ISR 2 + fgColor = RED; + print("[ Non-maskable Interrupt ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_breakpoint(intFrame_t* iframe) { // ISR 3 + fgColor = RED; + print("[ Breakpoint ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_overflow(intFrame_t* iframe) { // ISR 4 + fgColor = RED; + print("[ Overflow Error ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_boundRangeExceed(intFrame_t* iframe) { // ISR 5 + fgColor = RED; + print("[ Bound Range Exceeded ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_invalidOpcode(intFrame_t* iframe) { // ISR 6 + fgColor = RED; + print("[ Invalid OpCode ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_deviceNA(intFrame_t* iframe) { // ISR 7 + fgColor = RED; + print("[ Device Not Available ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_doubleFault(intFrame_t* iframe, uint32_t errCode) { // ISR 8 + fgColor = RED; + print("[ Double Fault : 0x"); + print(itoa(errCode, 16)); + print(" ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_coprocOverrun(intFrame_t* iframe) { // ISR 9 + fgColor = RED; + print("[ Coprocessor Segment Overrun ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_invalidTss(intFrame_t* iframe, uint32_t errCode) { // ISR 10 + fgColor = RED; + print("[ Invalid TSS : 0x"); + print(itoa(errCode, 16)); + print(" ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_noSegment(intFrame_t* iframe, uint32_t errCode) { // ISR 11 + fgColor = RED; + print("[ Segment Not Present : 0x"); + print(itoa(errCode, 16)); + print(" ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_stackSegmentFault(intFrame_t* iframe, uint32_t errCode) { // ISR 12 + fgColor = RED; + print("[ Stack-Segment Fault : 0x"); + print(itoa(errCode, 16)); + print(" ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_generalProtectionFault(intFrame_t* iframe, uint32_t errCode) { // ISR 13 + fgColor = RED; + print("[ General Protection Fault : 0x"); + print(itoa(errCode, 16)); + print(" ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_pageFault(intFrame_t* iframe, uint32_t errCode) { // ISR 14 + fgColor = RED; + print("[ Page Fault : 0x"); + print(itoa(errCode, 16)); + print(" ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_reserved(intFrame_t* iframe) { // ISR 15 + fgColor = RED; + print("[ Reserved Exception ]\n"); // Shouldn't get this one + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_floatingPointErr(intFrame_t* iframe) { // ISR 16 + fgColor = RED; + print("[ x87 FPU Floating-Point Error ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_alignCheck(intFrame_t* iframe, uint32_t errCode) { // ISR 17 + fgColor = RED; + print("[ Alignment Check : 0x"); + print(itoa(errCode, 16)); + print(" ]\n"); + __asm__ volatile ("cli; hlt"); +} + +__attribute__ ((interrupt)) +void excp_machineCheck(intFrame_t* iframe) { // ISR 18 + fgColor = RED; + print("[ Machine Check ]\n"); + __asm__ volatile ("cli; hlt"); +} + +// TODO: Add more (because there's always more) + +// Repetative isr definitions +void setupExceptions() { + for (uint8_t i = 0; i < 32; i++) { + idtSetGate(i, excp_reserved, TRAP_GATE_FLAGS); + } + + idtSetGate(0, excp_divideErr, TRAP_GATE_FLAGS); + idtSetGate(1, excp_debugErr, TRAP_GATE_FLAGS); + idtSetGate(2, excp_nmiInterrupt, TRAP_GATE_FLAGS); + idtSetGate(3, excp_breakpoint, TRAP_GATE_FLAGS); + idtSetGate(4, excp_overflow, TRAP_GATE_FLAGS); + idtSetGate(5, excp_boundRangeExceed, TRAP_GATE_FLAGS); + idtSetGate(6, excp_invalidOpcode, TRAP_GATE_FLAGS); + idtSetGate(7, excp_deviceNA, TRAP_GATE_FLAGS); + idtSetGate(8, excp_doubleFault, TRAP_GATE_FLAGS); + idtSetGate(9, excp_coprocOverrun, TRAP_GATE_FLAGS); + idtSetGate(10, excp_invalidTss, TRAP_GATE_FLAGS); + idtSetGate(11, excp_noSegment, TRAP_GATE_FLAGS); + idtSetGate(12, excp_stackSegmentFault, TRAP_GATE_FLAGS); + idtSetGate(13, excp_generalProtectionFault, TRAP_GATE_FLAGS); + idtSetGate(14, excp_pageFault, TRAP_GATE_FLAGS); + idtSetGate(15, excp_reserved, TRAP_GATE_FLAGS); + idtSetGate(16, excp_floatingPointErr, TRAP_GATE_FLAGS); + idtSetGate(17, excp_alignCheck, TRAP_GATE_FLAGS); + idtSetGate(18, excp_machineCheck, TRAP_GATE_FLAGS); + // Other are reserved +} + +#endif diff --git a/include/memory/addresses.h b/include/memory/addresses.h new file mode 100644 index 0000000..b3baa96 --- /dev/null +++ b/include/memory/addresses.h @@ -0,0 +1,7 @@ +#ifndef ADDRESSES_H +#define ADDRESSES_H + +#define MEMMAP_AREA 0x30000 +#define KERNEL_LOC 0x50000 + +#endif diff --git a/include/memory/physical.h b/include/memory/physical.h index 65a322a..14c4bbf 100644 --- a/include/memory/physical.h +++ b/include/memory/physical.h @@ -47,14 +47,20 @@ int32_t findFreeBlocks(uint32_t blockCount) { uint32_t block = 1 << j; if (!(memoryMap[i] & block)) { - uint32_t startBlock = i * 32 + block; - uint32_t freeBlocks = 0; + // uint32_t startBlock = i * 32 + block; + // uint32_t freeBlocks = 0; - for (uint32_t count = 0; count <= blockCount; count++) { - if (!testBlock(startBlock + count)) { - freeBlocks++; + for (uint32_t count = i, freeBlocks = 0; count < blockCount; count++) { + if (j + count > 31 && i + 1 <= maxBlocks / 32) { + if (!(memoryMap[i+1] & (1 << (j + count)))) { + freeBlocks++; + } + } else { + if (!(memoryMap[i] & (1 << (j + count)))) { + freeBlocks++; + } } - + if (freeBlocks == blockCount) { return i * 32 + j; } @@ -67,7 +73,7 @@ int32_t findFreeBlocks(uint32_t blockCount) { return -1; // No free memory found :/ } -void setupMemoryManager(uint32_t startAddress, uint32_t size) { +void setupPhysicalMemoryManager(uint32_t startAddress, uint32_t size) { memoryMap = (uint32_t*)startAddress; maxBlocks = size / BLOCK_SIZE; usedBlocks = maxBlocks; diff --git a/include/memory/virtual.h b/include/memory/virtual.h index 9cda24c..05bf1a7 100644 --- a/include/memory/virtual.h +++ b/include/memory/virtual.h @@ -3,6 +3,8 @@ #include #include +#include +#include #define PAGES_PER_TABLE 1024 #define TABLES_PER_DIRECTORY 1024 @@ -10,6 +12,11 @@ #define PD_INDEX(_vAddress) ((_vAddress) >> 22) #define PT_INDEX(_vAddress) (((_vAddress) >> 12) & 0x3ff) +#define PAGE_PHYS_ADDRESS(_dirEntry) (*(_dirEntry) & ~ 0xfff) +#define SET_ATTR(_entry, _attr) (*(_entry) |= (_attr)) +#define CLEAR_ATTR(_entry, _attr) (*(_entry) &= ~(_attr)) +#define TEST_ATTR(_entry, _attr) (*(_entry) & (_attr)) +#define SET_FRAME(_entry, _address) (*(_entry) = (*(_entry) & ~0x7ffff000) | (_address)) typedef uint32_t ptEntry_t; typedef uint32_t pdEntry_t; @@ -49,6 +56,151 @@ typedef struct { pdEntry_t entries[TABLES_PER_DIRECTORY]; } pageDirectory_t; -pageDirectory_t* currentPd = 0; +pageDirectory_t* currentPd = NULL; + +ptEntry_t* getPtEntry(pageTable_t* pt, uint32_t virtualAddress) { + if (pt) return &pt->entries[PT_INDEX(virtualAddress)]; + return NULL; +} + +pdEntry_t* getPdEntry(pageDirectory_t* pd, uint32_t virtualAddress) { + if (pd) return &pd->entries[PT_INDEX(virtualAddress)]; // It it supposed to be PT_INDEX, don't worry + return NULL; +} + +ptEntry_t* getPage(uint32_t virtualAddress) { + pageDirectory_t* pd = currentPd; + pdEntry_t* entry = &pd->entries[PD_INDEX(virtualAddress)]; + pageTable_t* table = (pageTable_t*)PAGE_PHYS_ADDRESS(entry); + ptEntry_t* page = &table->entries[PT_INDEX(virtualAddress)]; + return page; +} + +void* allocPage(ptEntry_t* page) { + void* block = allocBlocks(1); + if (block) { + SET_FRAME(page, (uint32_t)block); + SET_ATTR(page, PTE_PRESENT); + } + + return block; +} + +void freePage(ptEntry_t* page) { + void* address = (void*)PAGE_PHYS_ADDRESS(page); + if (address) { + freeBlocks(address, 1); + } + + CLEAR_ATTR(page, PTE_PRESENT); +} + +uint8_t setPageDirectory(pageDirectory_t* pd) { + if (!pd) { return 0; } // Nuh uh + currentPd = pd; + + // cr3 (Control register 3) - current page dir address + __asm__ volatile("movl %%eax, %%cr3" : : "a"(currentPd)); + + return 1; // Success +} + +void flushTlbEntry(uint32_t virtualAddress) { + __asm__ volatile("cli; invlpg (%0); sti" : : "r"(virtualAddress)); +} + +uint8_t mapPage(void* physicalAddr, void* virtualAddr) { + pageDirectory_t* pd = currentPd; + pdEntry_t* entry = &pd->entries[PD_INDEX((uint32_t)virtualAddr)]; + + if (!TEST_ATTR(entry, PTE_PRESENT)) { + // Assuming that the page is not allocated since it's not present + pageTable_t* table = (pageTable_t*)allocBlocks(1); + if (!table) return 0; // Out of memory!! + + // Clear page table (may be slow?) + memset(table, 0, sizeof(pageTable_t)); + + pdEntry_t* entry = &pd->entries[PD_INDEX((uint32_t)virtualAddr)]; + // Enable attributes and map in table + SET_ATTR(entry, PDE_PRESENT); + SET_ATTR(entry, PDE_READ_WRITE); + SET_FRAME(entry, (uint32_t)table); + } + + pageTable_t* table = (pageTable_t*)PAGE_PHYS_ADDRESS(entry); + ptEntry_t* page = &table->entries[PT_INDEX((uint32_t)virtualAddr)]; + + SET_ATTR(page, PTE_PRESENT); + SET_FRAME(page, (uint32_t)physicalAddr); + + return 1; // Success +} + +void unmapPage(void* virtualAddress) { + ptEntry_t* page = getPage((uint32_t)virtualAddress); + + SET_FRAME(page, 0); + CLEAR_ATTR(page, PDE_PRESENT); +} + +uint8_t setupVirtualMemoryManager() { + // Create a default page dir + pageDirectory_t* dir = (pageDirectory_t*)allocBlocks(3); + + if (!dir) { return 0; } // Out of memory + + memset(dir, 0, sizeof(pageDirectory_t)); + for (uint32_t i = 0; i < 1024; i++) { + dir->entries[i] = 0x02; // Supervisor, r/w, not present + } + + // Alloc default page table + pageTable_t* table = (pageTable_t*)allocBlocks(1); + if (!table) { return 0; } // Out of memory + + // Alloc a 3GB page + pageTable_t* table3gb = (pageTable_t*)allocBlocks(1); + if (!table) { return 0; } // Out of memory + + memset(table, 0, sizeof(pageTable_t)); + memset(table3gb, 0, sizeof(pageTable_t)); + + // Identity map first 4MB + for (uint32_t i = 0, frame = 0, virt = 0; i < 1024; i++, frame += PAGE_SIZE, virt += PAGE_SIZE) { + ptEntry_t page = 0; + SET_ATTR(&page, PTE_PRESENT); + SET_ATTR(&page, PTE_READ_WRITE); + SET_FRAME(&page, frame); + + table3gb->entries[PT_INDEX(virt)] = page; + } + + // Map kernel to 3GB+ + for (uint32_t i = 0, frame = KERNEL_LOC, virt = 0xc0000000; i < 1024; i++, frame += PAGE_SIZE, virt += PAGE_SIZE) { + ptEntry_t page = 0; + SET_ATTR(&page, PTE_PRESENT); + SET_ATTR(&page, PTE_READ_WRITE); + SET_FRAME(&page, frame); + + table->entries[PT_INDEX(virt)] = page; + } + + pdEntry_t* entry = &dir->entries[PD_INDEX(0xc0000000)]; + SET_ATTR(entry, PDE_PRESENT); + SET_ATTR(entry, PDE_READ_WRITE); + SET_FRAME(entry, (uint32_t)table); + + pdEntry_t* entry2 = &dir->entries[PD_INDEX(0x00000000)]; + SET_ATTR(entry2, PDE_PRESENT); + SET_ATTR(entry2, PDE_READ_WRITE); + SET_FRAME(entry2, (uint32_t)table3gb); + + setPageDirectory(dir); + + // ENABLE PAGING!!! Set PG bit 31 and PE bit 0 in cr0 + __asm__ volatile("movl %cr0, %eax; orl $0x80000001, %eax; movl %eax, %cr0"); + return 1; +} #endif diff --git a/src/kernel.c b/src/kernel.c index 0e98048..4e31a3a 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -15,42 +15,18 @@ #include #include #include +#include #define PROMPT "#> " char* filetable = (char*)0x7000; // Pretty sure we don't have to care about the bootloader at 0x7c00 -uint32_t memmapAddr = 0x30000; // Function declarations void printFiletable(char* ft); void printPhysicalMemmap(); void main() { - // Memory manager setup - uint32_t smapEntryCount = *(uint32_t*)0x8500; - smapEntry_t* smapEntries = (smapEntry_t*)0x8504; - uint32_t totalMemory = smapEntries[smapEntryCount - 1].baseAddress + smapEntries[smapEntryCount - 1].length - 1; - - setupMemoryManager(memmapAddr, totalMemory); - - for (uint32_t i = 0; i < smapEntryCount; i++) { - if (smapEntries[i].type == 1) { - initMemoryRegion(smapEntries[i].baseAddress, smapEntries[i].length); - } - } - - deinitMemoryRegion(0x1000, 0x9000); // Reserve kernel memory (under 0xa000) - deinitMemoryRegion(memmapAddr, maxBlocks / BLOCKS_PER_BYTE); - - // TODO: Find font in filetable - diskRead(46, 4, font); // Read font from disk - - clear(); - print("kern.\n\n"); - - diskRead(5, 1, filetable); // Read the filetable and store it in memory - - // Interrupts + // Interrupt setup setupIdt(); setupExceptions(); idtSetGate(0x80, syscallHandler, USR_INT_GAME_FLAGS); // Setup Syscall handler in IDT for int 0x80 @@ -63,6 +39,42 @@ void main() { setupRtc(); setupKeyboard(); + // Physical memory manager setup + uint32_t smapEntryCount = *(uint32_t*)0x8500; + smapEntry_t* smapEntries = (smapEntry_t*)0x8504; + uint32_t totalMemory = smapEntries[smapEntryCount - 1].baseAddress + smapEntries[smapEntryCount - 1].length - 1; + + setupPhysicalMemoryManager(MEMMAP_AREA, totalMemory); + + for (uint32_t i = 0; i < smapEntryCount; i++) { + if (smapEntries[i].type == 1) { + initMemoryRegion(smapEntries[i].baseAddress, smapEntries[i].length); + } + } + + deinitMemoryRegion(0x1000, 0x9000); // Reserve kernel memory (under 0xa000) + deinitMemoryRegion(MEMMAP_AREA, maxBlocks / BLOCKS_PER_BYTE); + + // Virtual memory manager setup + setupVirtualMemoryManager(); // TODO: Check if it succeeds + + // Identity map VBE framebuffer + uint32_t fbSize = (gfxMode->yRes * gfxMode->linearBytesPerScanline) / PAGE_SIZE; + if (fbSize % PAGE_SIZE > 0) { fbSize++; } + fbSize *= 2; // Double for hardware + for (uint32_t i = 0, fbStart = gfxMode->physicalBasePtr; i < fbSize; i++, fbStart += PAGE_SIZE) { + mapPage((void*)fbStart, (void*)fbStart); + } + deinitMemoryRegion(gfxMode->physicalBasePtr, fbSize * BLOCK_SIZE); + + // Screen setup + diskRead(46, 4, font); // TODO: Find font in filetable + + clear(); + print("kern.\n\n"); + + diskRead(5, 1, filetable); // Read the filetable and store it in memory + // Run Interactive Shell Program // TODO: Make it in another file while (1) { diff --git a/src/stage2.asm b/src/stage2.asm index e9b4dcc..d237ab1 100644 --- a/src/stage2.asm +++ b/src/stage2.asm @@ -372,9 +372,9 @@ DATA_SEG equ GDT.dataDescriptor - GDT.start ;; VBE Stuff ;; !! IF YOU WANT TO SELECT ANY VALUE IN THE SETUP, SET THESE TO ZERO !! -width: dw 1920 -height: dw 1080 -bpp: db 32 +width: dw 0 +height: dw 0 +bpp: db 0 offset: dw 0 _segment: dw 0 ; segment is a keyword diff --git a/todo.txt b/todo.txt index e230abb..d6505a0 100644 --- a/todo.txt +++ b/todo.txt @@ -111,12 +111,12 @@ Legend: - [ ] stdin - [ ] stdout - [ ] stderr -- [ ] Memory manager +- [x] Memory manager - [x] Memory map - [x] Print mem map - [x] Physical - - [ ] Virtual - - [ ] Paging + - [x] Virtual + - [x] Paging - [ ] User (or system) programs - [x] Loading them - [x] Running them