53 lines
1.2 KiB
C
53 lines
1.2 KiB
C
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include "bitmap/bitmap.h"
|
|
#include "spinlock/spinlock.h"
|
|
#include "bootinfo/bootinfo.h"
|
|
#include "vmm.h"
|
|
#include "kprintf.h"
|
|
#include "util/util.h"
|
|
#include "hal/hal.h"
|
|
#include "pmm/pmm.h"
|
|
#include "paging/paging.h"
|
|
|
|
#define POS_ENSURE 0x40000000
|
|
|
|
#define PAGE_SIZE 0x1000
|
|
|
|
VirtMem VIRT_MEM;
|
|
|
|
void vmm_init(void) {
|
|
spinlock_init(&VIRT_MEM.spinlock);
|
|
BitMap *bm = &VIRT_MEM.self;
|
|
|
|
size_t targetpos = _DIV_ROUNDUP(BOOT_INFO.hhdm_off - BOOT_INFO.memmap_total - POS_ENSURE, PAGE_SIZE) * PAGE_SIZE;
|
|
bm->init = false;
|
|
|
|
bm->mem_start = targetpos;
|
|
bm->nblocks = _DIV_ROUNDUP(BOOT_INFO.memmap_total, BITMAP_BLOCK_SIZE);
|
|
bm->nbytes = _DIV_ROUNDUP(bm->nblocks, 8);
|
|
|
|
uint64_t pagesrequired = _DIV_ROUNDUP(bm->nbytes, BITMAP_BLOCK_SIZE);
|
|
bm->map = (uint8_t *)vmm_alloc(pagesrequired);
|
|
hal_memset(bm->map, 0, bm->nbytes);
|
|
|
|
bm->init = true;
|
|
}
|
|
|
|
void *vmm_alloc(size_t pages) {
|
|
size_t phys = (size_t)pmm_alloc(pages);
|
|
uint64_t out = phys + BOOT_INFO.hhdm_off;
|
|
return (void *)out;
|
|
}
|
|
|
|
|
|
void vmm_free(void *ptr, size_t pages) {
|
|
size_t phys = paging_virt2phys((size_t)ptr);
|
|
if (!phys) {
|
|
ERR("vmm", "could not find phys addr for %p\n", ptr);
|
|
hal_hang();
|
|
}
|
|
pmm_free(phys, pages);
|
|
}
|
|
|