Virtual memory and dlmalloc
This commit is contained in:
52
kernel/vmm/vmm.c
Normal file
52
kernel/vmm/vmm.c
Normal file
@ -0,0 +1,52 @@
|
||||
#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);
|
||||
}
|
||||
|
19
kernel/vmm/vmm.h
Normal file
19
kernel/vmm/vmm.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef VMM_VMM_H_
|
||||
#define VMM_VMM_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include "bitmap/bitmap.h"
|
||||
#include "spinlock/spinlock.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);
|
||||
|
||||
#endif // VMM_VMM_H_
|
Reference in New Issue
Block a user