Kernel: Continue moving utilities to a separate subdirectory
This commit is contained in:
parent
e21b608af4
commit
5abd8814e3
@ -1,30 +1,10 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
23
kernel/include/utils/Addresses.h
Normal file
23
kernel/include/utils/Addresses.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
}
|
@ -6,6 +6,7 @@
|
||||
#include "memory/PMM.h"
|
||||
#include "misc/utils.h"
|
||||
#include "std/string.h"
|
||||
#include "utils/Addresses.h"
|
||||
#include "utils/Registers.h"
|
||||
|
||||
static PageTable* kernel_pml4;
|
||||
@ -61,7 +62,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(current_pml4, vaddr);
|
||||
if (!pde) return; // Already unmapped
|
||||
@ -88,7 +89,7 @@ void VMM::remap(uint64_t vaddr, int flags)
|
||||
|
||||
uint64_t VMM::get_physical(uint64_t vaddr)
|
||||
{
|
||||
PageDirectoryEntry* pde = find_pde(current_pml4, Utilities::round_down_to_nearest_page(vaddr));
|
||||
PageDirectoryEntry* pde = find_pde(current_pml4, round_down_to_nearest_page(vaddr));
|
||||
if (!pde) return UINT64_MAX; // Not mapped
|
||||
|
||||
return pde->get_address() | (vaddr % PAGE_SIZE);
|
||||
@ -96,7 +97,7 @@ uint64_t VMM::get_physical(uint64_t vaddr)
|
||||
|
||||
uint64_t VMM::get_flags(uint64_t vaddr)
|
||||
{
|
||||
PageDirectoryEntry* pde = find_pde(current_pml4, Utilities::round_down_to_nearest_page(vaddr));
|
||||
PageDirectoryEntry* pde = find_pde(current_pml4, round_down_to_nearest_page(vaddr));
|
||||
if (!pde) return 0; // Not mapped
|
||||
|
||||
uint64_t flags = 0;
|
||||
@ -107,7 +108,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(current_pml4, vaddr);
|
||||
bool will_flush_tlb = true;
|
||||
if (!pde)
|
||||
@ -121,7 +122,7 @@ void VMM::map(uint64_t vaddr, uint64_t paddr, int flags)
|
||||
pde = create_pde_if_not_exists(current_pml4, vaddr);
|
||||
}
|
||||
|
||||
pde->set_address(Utilities::round_down_to_nearest_page(paddr));
|
||||
pde->set_address(round_down_to_nearest_page(paddr));
|
||||
if (flags & User) propagate_user(current_pml4, vaddr);
|
||||
else
|
||||
pde->user = false;
|
||||
|
@ -13,6 +13,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)
|
||||
{
|
||||
@ -90,8 +91,8 @@ ELFImage* ELFLoader::load_elf_from_vfs(VFS::Node* node)
|
||||
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, MAP_READ_WRITE) +
|
||||
void* buffer = (void*)((uint64_t)MemoryManager::get_pages_at(round_down_to_nearest_page(phdr.p_vaddr),
|
||||
pages, MAP_READ_WRITE) +
|
||||
(phdr.p_vaddr % PAGE_SIZE));
|
||||
|
||||
VMM::apply_address_space();
|
||||
@ -193,7 +194,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);
|
||||
}
|
@ -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;
|
||||
@ -140,7 +141,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;
|
||||
@ -167,7 +168,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;
|
||||
|
Loading…
Reference in New Issue
Block a user