72 lines
1.6 KiB
C
72 lines
1.6 KiB
C
/*
|
|
* @author xamidev <xamidev@riseup.net>
|
|
* @brief Virtual memory manager
|
|
* @license GPL-3.0-only
|
|
*/
|
|
|
|
/*
|
|
The VMM (virtual memory manager) will have two roles:
|
|
- mapping pages
|
|
- unmapping pages
|
|
in a specified virtual space
|
|
|
|
compared to the PMM which allocs/frees 4kb frames ("physical pages").
|
|
*/
|
|
|
|
#include "vmm.h"
|
|
#include "paging.h"
|
|
#include <stddef.h>
|
|
#include "pmm.h"
|
|
#include <kernel.h>
|
|
|
|
void* vmm_pt_root = 0;
|
|
|
|
// Linked list head for virtual memory objects
|
|
struct vm_object* vm_objs = NULL;
|
|
|
|
|
|
uint64_t convert_x86_vm_flags(size_t flags)
|
|
{
|
|
uint64_t value = 0;
|
|
if (flags & VM_FLAG_WRITE)
|
|
{
|
|
value |= PTE_WRITABLE;
|
|
}
|
|
if (flags & VM_FLAG_USER)
|
|
{
|
|
value |= PTE_USER;
|
|
}
|
|
if ((flags & VM_FLAG_EXEC) == 0)
|
|
{
|
|
value |= PTE_NOEXEC;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
extern uint64_t *kernel_pml4;
|
|
|
|
void vmm_setup_pt_root()
|
|
{
|
|
// We alloc a physical page (frame) for the pointer, then map it
|
|
// to virt (pointer)
|
|
uintptr_t phys = pmm_alloc();
|
|
vmm_pt_root = (void*)kernel_pml4;
|
|
paging_map_page(kernel_pml4, (uint64_t)vmm_pt_root, phys, convert_x86_vm_flags(VM_FLAG_WRITE | VM_FLAG_EXEC));
|
|
DEBUG("VMM setup: vmm_pt_root=0x%p (phys=0x%p)", vmm_pt_root, phys);
|
|
}
|
|
|
|
/* void* vmm_alloc(size_t length, size_t flags)
|
|
{
|
|
// We will try to allocate at least length bytes, which have to be rounded UP to
|
|
// the next page so its coherent with the PMM
|
|
size_t len = ALIGN_UP(length, PAGE_SIZE);
|
|
|
|
// Need to implement this (as linked list)
|
|
// but for now kernel heap is sufficient
|
|
// The VMM will prob be more useful when we have userspace
|
|
} */
|
|
|
|
void vmm_init()
|
|
{
|
|
vmm_setup_pt_root();
|
|
} |