Rework x86_64 paging and vmm
This commit is contained in:
@ -8,45 +8,30 @@
|
||||
#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_map_kern_page(uint64_t virtaddr, uint64_t physaddr, uint32_t flags) {
|
||||
#if defined(__x86_64__)
|
||||
hal_vmm_map_page(KERNEL_CR3, virtaddr, physaddr, flags);
|
||||
#else
|
||||
# error "arch"
|
||||
#endif
|
||||
}
|
||||
|
||||
void vmm_unmap_kern_page(uint64_t virtaddr, uint64_t physaddr) {
|
||||
#if defined(__x86_64__)
|
||||
hal_vmm_unmap_page(KERNEL_CR3, virtaddr, physaddr);
|
||||
#else
|
||||
# error "arch"
|
||||
#endif
|
||||
}
|
||||
|
||||
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;
|
||||
hal_vmm_init();
|
||||
|
||||
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);
|
||||
LOG("vmm", "init\n");
|
||||
}
|
||||
|
||||
|
@ -2,18 +2,17 @@
|
||||
#define VMM_VMM_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include "bitmap/bitmap.h"
|
||||
#include "spinlock/spinlock.h"
|
||||
#include "bootinfo/bootinfo.h"
|
||||
|
||||
typedef struct {
|
||||
SpinLock spinlock;
|
||||
BitMap self;
|
||||
} VirtMem;
|
||||
|
||||
extern VirtMem VIRT_MEM;
|
||||
|
||||
void vmm_init(void);
|
||||
void *vmm_alloc(size_t pages);
|
||||
void vmm_free(void *ptr, size_t pages);
|
||||
|
||||
#define VIRT(X) ((void *)(BOOT_INFO.hhdm_off + (uint64_t)(X)))
|
||||
|
||||
#endif // VMM_VMM_H_
|
||||
|
Reference in New Issue
Block a user