diff --git a/kernel/include/misc/utils.h b/kernel/include/misc/utils.h index c7660111..cc8974e5 100644 --- a/kernel/include/misc/utils.h +++ b/kernel/include/misc/utils.h @@ -1,30 +1,10 @@ #pragma once #include -#ifndef PAGE_SIZE -#define PAGE_SIZE 4096 -#endif - namespace Utilities { inline uint64_t get_blocks_from_size(uint64_t blocksize, uint64_t size) { return (size + (blocksize - 1)) / blocksize; } - - inline uint64_t get_top_of_stack(uint64_t bottom, uint64_t stack_pages) - { - return bottom + (stack_pages * PAGE_SIZE) - sizeof(uintptr_t); - } - - inline uint64_t round_down_to_nearest_page(uint64_t addr) - { - return addr - (addr % PAGE_SIZE); - } - - inline uint64_t round_up_to_nearest_page(uint64_t addr) - { - if (addr % PAGE_SIZE) return addr + (PAGE_SIZE - (addr % PAGE_SIZE)); - return addr; - } } \ No newline at end of file diff --git a/kernel/include/utils/Addresses.h b/kernel/include/utils/Addresses.h new file mode 100644 index 00000000..e006d7a0 --- /dev/null +++ b/kernel/include/utils/Addresses.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +#ifndef PAGE_SIZE +#define PAGE_SIZE 4096 +#endif + +inline uintptr_t get_top_of_stack(uintptr_t bottom, uintptr_t stack_pages) +{ + return bottom + (stack_pages * PAGE_SIZE) - sizeof(uintptr_t); +} + +inline uintptr_t round_down_to_nearest_page(uintptr_t addr) +{ + return addr - (addr % PAGE_SIZE); +} + +inline uintptr_t round_up_to_nearest_page(uintptr_t addr) +{ + if (addr % PAGE_SIZE) return addr + (PAGE_SIZE - (addr % PAGE_SIZE)); + return addr; +} \ No newline at end of file diff --git a/kernel/src/memory/VMM.cpp b/kernel/src/memory/VMM.cpp index 7e3e696e..35e53765 100644 --- a/kernel/src/memory/VMM.cpp +++ b/kernel/src/memory/VMM.cpp @@ -6,6 +6,7 @@ #include "memory/PMM.h" #include "misc/utils.h" #include "std/string.h" +#include "utils/Addresses.h" #include "utils/Registers.h" // FIXME: There is a lot of duplicate code in this file. This should probably be refactored. @@ -19,7 +20,7 @@ void VMM::init() void VMM::unmap(uint64_t vaddr) { - vaddr = Utilities::round_down_to_nearest_page(vaddr); + vaddr = round_down_to_nearest_page(vaddr); PageDirectoryEntry* pde = find_pde(PML4, vaddr); if (!pde) return; // Already unmapped @@ -30,7 +31,7 @@ void VMM::unmap(uint64_t vaddr) uint64_t VMM::get_physical(uint64_t vaddr) { - PageDirectoryEntry* pde = find_pde(PML4, Utilities::round_down_to_nearest_page(vaddr)); + PageDirectoryEntry* pde = find_pde(PML4, round_down_to_nearest_page(vaddr)); if (!pde) return UINT64_MAX; // Not mapped return pde->get_address() | (vaddr % PAGE_SIZE); @@ -38,7 +39,7 @@ uint64_t VMM::get_physical(uint64_t vaddr) uint64_t VMM::get_flags(uint64_t vaddr) { - PageDirectoryEntry* pde = find_pde(PML4, Utilities::round_down_to_nearest_page(vaddr)); + PageDirectoryEntry* pde = find_pde(PML4, round_down_to_nearest_page(vaddr)); if (!pde) return 0; // Not mapped uint64_t flags = 0; @@ -49,7 +50,7 @@ uint64_t VMM::get_flags(uint64_t vaddr) void VMM::map(uint64_t vaddr, uint64_t paddr, int flags) { - vaddr = Utilities::round_down_to_nearest_page(vaddr); + vaddr = round_down_to_nearest_page(vaddr); PageDirectoryEntry* pde = find_pde(PML4, vaddr); bool will_flush_tlb = true; if (!pde) @@ -64,7 +65,7 @@ void VMM::map(uint64_t vaddr, uint64_t paddr, int flags) will_flush_tlb = false; } - pde->set_address(Utilities::round_down_to_nearest_page(paddr)); + pde->set_address(round_down_to_nearest_page(paddr)); if (flags & User) propagate_user(PML4, vaddr); if (flags & ReadWrite) propagate_read_write(PML4, vaddr); if (will_flush_tlb) flush_tlb(vaddr); diff --git a/kernel/src/sys/elf/ELFLoader.cpp b/kernel/src/sys/elf/ELFLoader.cpp index 4ca64147..93ccaa3e 100644 --- a/kernel/src/sys/elf/ELFLoader.cpp +++ b/kernel/src/sys/elf/ELFLoader.cpp @@ -12,6 +12,7 @@ #include "std/stdlib.h" #include "std/string.h" #include "sys/elf/ELF.h" +#include "utils/Addresses.h" static const char* format_permissions(uint32_t flags) { @@ -86,10 +87,10 @@ ELFImage* ELFLoader::load_elf_from_vfs(VFS::Node* node) phdr.p_filesz, phdr.p_memsz, format_permissions(phdr.p_flags)); ASSERT(phdr.p_vaddr); uint64_t pages = Utilities::get_blocks_from_size(PAGE_SIZE, (phdr.p_vaddr % PAGE_SIZE) + phdr.p_memsz); - void* buffer = (void*)((uint64_t)MemoryManager::get_pages_at( - Utilities::round_down_to_nearest_page(phdr.p_vaddr), pages, - phdr.p_flags & 2 ? MAP_READ_WRITE | MAP_USER : MAP_USER) + - (phdr.p_vaddr % PAGE_SIZE)); + void* buffer = + (void*)((uint64_t)MemoryManager::get_pages_at(round_down_to_nearest_page(phdr.p_vaddr), pages, + phdr.p_flags & 2 ? MAP_READ_WRITE | MAP_USER : MAP_USER) + + (phdr.p_vaddr % PAGE_SIZE)); VFS::read(node, phdr.p_offset, phdr.p_filesz, (char*)buffer); memset((void*)((uint64_t)buffer + phdr.p_filesz), 0, phdr.p_memsz - phdr.p_filesz); image = (ELFImage*)krealloc(image, (sizeof(ELFImage) - sizeof(ELFSection)) + @@ -180,7 +181,7 @@ void ELFLoader::release_elf_image(ELFImage* image) { ELFSection& section = image->sections[i]; kdbgln("Freeing up section %lx, was using %ld pages", section.base, section.pages); - MemoryManager::release_pages((void*)Utilities::round_down_to_nearest_page(section.base), section.pages); + MemoryManager::release_pages((void*)round_down_to_nearest_page(section.base), section.pages); } kfree(image); } \ No newline at end of file diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index d2f8aac7..6a4d8ac5 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -14,6 +14,7 @@ #include "sys/elf/ELFLoader.h" #include "thread/PIT.h" #include "thread/Task.h" +#include "utils/Addresses.h" #include "utils/Registers.h" static uint64_t task_num = 0; @@ -35,7 +36,7 @@ void Scheduler::init() memset(&idle_task, 0, sizeof(Task)); idle_task.id = free_tid++; idle_task.regs.rip = (uint64_t)idle_task_function; - idle_task.regs.rsp = Utilities::get_top_of_stack((uint64_t)MemoryManager::get_page(), 1); + idle_task.regs.rsp = get_top_of_stack((uint64_t)MemoryManager::get_page(), 1); idle_task.regs.cs = 0x08; idle_task.regs.ss = 0x10; idle_task.regs.rflags = (1 << 21) | (1 << 9); @@ -67,7 +68,7 @@ void Scheduler::add_kernel_task(void (*task)(void)) new_task->regs.rip = (uint64_t)task; new_task->allocated_stack = (uint64_t)MemoryManager::get_pages(TASK_PAGES_IN_STACK); // 16 KB is enough for everyone, right? - new_task->regs.rsp = Utilities::get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); + new_task->regs.rsp = get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); new_task->regs.cs = 0x08; new_task->regs.ss = 0x10; new_task->regs.ds = 0x10; @@ -96,7 +97,7 @@ void Scheduler::add_user_task(void* task) new_task->regs.rip = (uint64_t)task; new_task->allocated_stack = (uint64_t)MemoryManager::get_pages( TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? - new_task->regs.rsp = Utilities::get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); + new_task->regs.rsp = get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); new_task->regs.cs = 0x18 | 0x03; new_task->regs.ss = 0x20 | 0x03; new_task->regs.ds = 0x20 | 0x03; @@ -138,7 +139,7 @@ void Scheduler::load_user_task(const char* filename) new_task->image = image; new_task->allocated_stack = (uint64_t)MemoryManager::get_pages( TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? - new_task->regs.rsp = Utilities::get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); + new_task->regs.rsp = get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); new_task->regs.cs = 0x18 | 0x03; new_task->regs.ss = 0x20 | 0x03; new_task->regs.ds = 0x20 | 0x03; @@ -164,7 +165,7 @@ void Scheduler::reset_task(Task* task, ELFImage* new_image) task->state = task->Running; task->regs.rip = new_image->entry; task->image = new_image; - task->regs.rsp = Utilities::get_top_of_stack(task->allocated_stack, TASK_PAGES_IN_STACK); + task->regs.rsp = get_top_of_stack(task->allocated_stack, TASK_PAGES_IN_STACK); task->regs.cs = 0x18 | 0x03; task->regs.ss = 0x20 | 0x03; task->regs.ds = 0x20 | 0x03;