Move vmm fully into hal

This commit is contained in:
2025-09-02 07:51:02 +02:00
parent 0fb3a1ca75
commit 920de10025
13 changed files with 76 additions and 186 deletions

View File

@ -43,7 +43,6 @@ SRCFILES += $(call GRABSRC, \
bootinfo \
spinlock \
term \
vmm \
dlmalloc \
vfs \
storedev \

View File

@ -13,11 +13,7 @@ BootInfo BOOT_INFO;
static volatile struct limine_paging_mode_request paging_req = {
.id = LIMINE_PAGING_MODE_REQUEST,
.revision = 0,
#if defined(__x86_64__)
.mode = LIMINE_PAGING_MODE_X86_64_4LVL,
#else
# error "Paging mode is unknown for this architecture"
#endif
};
DEFINE_REQ(kernel_address, KERNEL_ADDRESS);
@ -38,9 +34,7 @@ void bootinfo_init(void) {
BOOT_INFO.modules = modulesres;
struct limine_paging_mode_response *pagingres = paging_req.response;
#if defined(__x86_64__)
if (pagingres->mode != LIMINE_PAGING_MODE_X86_64_4LVL) {
#endif
hal_hang();
}

View File

@ -6,9 +6,9 @@
#include "kprintf.h"
#include "bitmap/bitmap.h"
#include "util/util.h"
#include "vmm/vmm.h"
#include "pmm/pmm.h"
#include "malloc.h"
#include "bootinfo/bootinfo.h"
#define USE_DL_PREFIX 1
#define LACKS_SYS_TYPES_H 1

View File

@ -21,13 +21,11 @@ char *hal_strchr(const char *s, int c);
void hal_init_withmalloc(void);
void hal_wait(uint32_t ms);
#if defined(__x86_64__)
# define HAL_PAGE_SIZE 0x1000
# include "x86_64/vmm.h"
# include "x86_64/proc.h"
# include "x86_64/switch.h"
# include "x86_64/paging.h"
# include "x86_64/cpu.h"
#endif
#define HAL_PAGE_SIZE 0x1000
#include "x86_64/vmm.h"
#include "x86_64/switch.h"
#include "x86_64/paging.h"
#include "x86_64/cpu.h"
#include "x86_64/intr.h"
#endif // KERNEL_HAL_HAL_H_

View File

@ -1,15 +0,0 @@
#ifndef HAL_PROC_H_
#define HAL_PROC_H_
#include <stdint.h>
#include "intr.h"
#include "vmm.h"
typedef struct {
uint64_t fsbase;
IntrStackFrame trapframe;
uint8_t *kstack;
PgTable *cr3;
} ProcPlatformData;
#endif // HAL_PROC_H_

View File

@ -7,8 +7,10 @@
#include "paging.h"
#include "proc/proc.h"
#include "kprintf.h"
#include "spinlock/spinlock.h"
PgTable *KERNEL_CR3 = NULL;
SpinLock spinlock;
PgTable *hal_vmm_current_cr3(void) {
PgTable *cr3;
@ -49,7 +51,9 @@ void hal_vmm_map_page(PgTable *pml4, uint64_t virtaddr, uint64_t physaddr, uint3
uint64_t *pml1 = hal_vmm_nexttable((uint64_t *)pml2, pi.pml2);
uint64_t *pte = &pml1[pi.pml1];
spinlock_acquire(&spinlock);
*pte = (physaddr & ~0xFFF) | (flags & 0x7);
spinlock_release(&spinlock);
}
void hal_vmm_unmap_page(PgTable *pml4, uint64_t virtaddr, uint64_t physaddr) {
@ -60,7 +64,9 @@ void hal_vmm_unmap_page(PgTable *pml4, uint64_t virtaddr, uint64_t physaddr) {
uint64_t *pml1 = hal_vmm_nexttable((uint64_t *)pml2, pi.pml2);
uint64_t *pte = &pml1[pi.pml1];
spinlock_acquire(&spinlock);
*pte &= ~HAL_PG_PRESENT;
spinlock_release(&spinlock);
}
void hal_vmm_map_range(PgTable *cr3, void *virtstart, void *physstart, size_t size, uint32_t flags) {
@ -113,6 +119,6 @@ PgTable *hal_vmm_userproc_pml4(Proc *proc) {
}
void hal_vmm_init(void) {
spinlock_init(&spinlock);
KERNEL_CR3 = hal_vmm_current_cr3();
kprintf("KERNEL_CR3 = %p\n", KERNEL_CR3);
}

View File

@ -3,8 +3,20 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "compiler/attr.h"
#define VIRT(X) ((void *)(BOOT_INFO.hhdm_off + (uint64_t)(X)))
typedef struct VasRange {
struct VasRange *next;
uint8_t *virtstart;
uint8_t *physstart;
size_t size;
uint8_t pgflags;
} PACKED VasRange;
struct Proc;
enum {

View File

@ -4,7 +4,6 @@
#include "hal/hal.h"
#include "bootinfo/bootinfo.h"
#include "pmm/pmm.h"
#include "vmm/vmm.h"
#include "term/term.h"
#include "dlmalloc/malloc.h"
#include "vfs/vfs.h"
@ -61,7 +60,7 @@ void kmain(void) {
log_bootinfo();
hal_init();
pmm_init();
vmm_init();
hal_vmm_init();
dlmalloc_check();
storedev_init();
baseimg_init();

View File

@ -2,7 +2,6 @@
#include <stdlib.h>
#include <stdbool.h>
#include "hal/hal.h"
#include "vmm/vmm.h"
#include "spinlock/spinlock.h"
#include "proc.h"
#include "dlmalloc/malloc.h"
@ -35,9 +34,7 @@ bool proc_checkelf(uint8_t *elf) {
}
ElfAuxval proc_load_elf_segs(Proc *proc, uint8_t *data) {
#if defined(__x86_64__)
PgTable *vas = proc->platformdata.cr3;
#endif
ElfAuxval aux = {0};
Elf64_Ehdr *elfhdr = (Elf64_Ehdr *)data;
@ -62,18 +59,14 @@ ElfAuxval proc_load_elf_segs(Proc *proc, uint8_t *data) {
hal_memset(VIRT(physaddr), 0, phdr->p_memsz);
hal_memcpy(VIRT(physaddr) + off, (data + phdr->p_offset), phdr->p_filesz);
#if defined(__x86_64__)
uint32_t pgflags = HAL_PG_USER | HAL_PG_RW | HAL_PG_PRESENT;
hal_vmm_map_range(VIRT(vas), virtaddr, physaddr, blocks * HAL_PAGE_SIZE, pgflags);
#endif
VasRange *range = dlmalloc(sizeof(*range));
range->virtstart = virtaddr;
range->physstart = physaddr;
range->size = blocks * HAL_PAGE_SIZE;
#if defined(__x86_64__)
range->pgflags = pgflags;
#endif
LL_APPEND(proc->vas, range);
} break;
@ -98,7 +91,6 @@ Proc *proc_spawnkern(void (*ent)(void), char *name) {
uint8_t *sp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
#if defined(__x86_64__)
proc->platformdata.kstack = sp;
hal_memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe));
@ -110,7 +102,6 @@ Proc *proc_spawnkern(void (*ent)(void), char *name) {
proc->platformdata.cr3 = hal_vmm_current_cr3();
proc->state = PROC_READY;
proc->pid = pids++;
#endif
return proc;
}
@ -136,7 +127,6 @@ Proc *proc_spawnuser(char *mountpoint, char *path) {
uint8_t *sp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
uint8_t *spbase = sp - PROC_STACKSIZE;
#if defined(__x86_64__)
proc->platformdata.kstack = sp;
hal_memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe));
@ -158,7 +148,6 @@ Proc *proc_spawnuser(char *mountpoint, char *path) {
proc->platformdata.trapframe.rflags = 0x202;
proc->platformdata.trapframe.cs = 0x18 | 0x3;
proc->platformdata.trapframe.rip = aux.entry;
#endif
proc->state = PROC_READY;
proc->pid = pids++;
@ -186,9 +175,7 @@ void proc_reaper(void) {
LL_REMOVE(PROCS.procs, zombie);
#if defined(__x86_64__)
pmm_free((uintptr_t)(zombie->platformdata.kstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
#endif
if (!zombie->kern) {
VasRange *vashead = zombie->vas;
@ -196,9 +183,7 @@ void proc_reaper(void) {
while (vashead) {
VasRange *tmp = vashead;
vashead = vashead->next;
#if defined(__x86_64__)
hal_vmm_unmap_range(zombie->platformdata.cr3, tmp->virtstart, tmp->physstart, tmp->size);
#endif
// first pmm mapping is for the elf itself
if (i == 0) {
pmm_free((uintptr_t)tmp->physstart, tmp->size / HAL_PAGE_SIZE);
@ -207,9 +192,7 @@ void proc_reaper(void) {
i++;
}
#if defined(__x86_64__)
pmm_free((uintptr_t)zombie->platformdata.cr3, 1);
#endif
}
dlfree(zombie);
} else {
@ -226,20 +209,16 @@ void proc_sched(void *cpustate) {
proc_reaper();
}
#if defined(__x86_64__)
IntrStackFrame *frame = cpustate;
PROCS.current->platformdata.trapframe = *frame;
PROCS.current->platformdata.fsbase = hal_cpu_rdmsr(HAL_CPU_FSBASE);
#endif
PROCS.current = proc_nextready();
PROCS.current->state = PROC_RUNNING;
#if defined(__x86_64__)
hal_cpu_wrmsr(HAL_CPU_FSBASE, PROCS.current->platformdata.fsbase);
hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3);
#endif
}
void proc_kill(Proc *proc) {
@ -285,7 +264,5 @@ void proc_init(void) {
proc_register(proc_spawnkern(&proc_status, "status"));
proc_register(proc_spawnuser("base", "/bin/hello"));
#if defined(__x86_64__)
hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3);
#endif
}

View File

@ -3,7 +3,6 @@
#include <stdint.h>
#include "hal/hal.h"
#include "vmm/vmm.h"
#include "spinlock/spinlock.h"
#include "bitmap/bitmap.h"
@ -14,6 +13,13 @@
#define PROC_MAX 0x100 // max amount of processes
typedef struct {
uint64_t fsbase;
IntrStackFrame trapframe;
uint8_t *kstack;
PgTable *cr3;
} ProcPlatformData;
enum {
PROC_READY,
PROC_RUNNING,

View File

@ -8,12 +8,7 @@ typedef struct {
atomic_bool lock;
} SpinLock;
// Spin more efficiently - cpu dependant
#if defined(__x86_64__)
# define SPINLOCK_HINT() asm volatile("pause")
#else
# define SPINLOCK_HINT()
#endif
#define SPINLOCK_HINT() asm volatile("pause")
void spinlock_init(SpinLock *sl);
void spinlock_acquire(SpinLock *sl);

View File

@ -1,53 +0,0 @@
#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"
VirtMem VIRT_MEM;
void vmm_map_kern_page(uint64_t virtaddr, uint64_t physaddr, uint32_t flags) {
spinlock_acquire(&VIRT_MEM.spinlock);
#if defined(__x86_64__)
hal_vmm_map_page(KERNEL_CR3, virtaddr, physaddr, flags);
#endif
spinlock_release(&VIRT_MEM.spinlock);
}
void vmm_unmap_kern_page(uint64_t virtaddr, uint64_t physaddr) {
spinlock_acquire(&VIRT_MEM.spinlock);
#if defined(__x86_64__)
hal_vmm_unmap_page(KERNEL_CR3, virtaddr, physaddr);
#endif
spinlock_release(&VIRT_MEM.spinlock);
}
void vmm_map_kern_range(void *virtstart, void *physstart, size_t size, uint32_t flags) {
spinlock_acquire(&VIRT_MEM.spinlock);
#if defined(__x86_64__)
hal_vmm_map_range(KERNEL_CR3, virtstart, physstart, size, flags);
#endif
spinlock_release(&VIRT_MEM.spinlock);
}
void vmm_unmap_kern_range(void *virtstart, void *physstart, size_t size) {
spinlock_acquire(&VIRT_MEM.spinlock);
#if defined(__x86_64__)
hal_vmm_unmap_range(KERNEL_CR3, virtstart, physstart, size);
#endif
spinlock_release(&VIRT_MEM.spinlock);
}
void vmm_init(void) {
spinlock_init(&VIRT_MEM.spinlock);
hal_vmm_init();
LOG("vmm", "init\n");
}

View File

@ -1,28 +0,0 @@
#ifndef VMM_VMM_H_
#define VMM_VMM_H_
#include <stddef.h>
#include "spinlock/spinlock.h"
#include "bootinfo/bootinfo.h"
#include "compiler/attr.h"
typedef struct {
SpinLock spinlock;
} VirtMem;
typedef struct VasRange {
struct VasRange *next;
uint8_t *virtstart;
uint8_t *physstart;
size_t size;
uint8_t pgflags;
} PACKED VasRange;
extern VirtMem VIRT_MEM;
void vmm_init(void);
#define VIRT(X) ((void *)(BOOT_INFO.hhdm_off + (uint64_t)(X)))
#endif // VMM_VMM_H_