Move vmm fully into hal
This commit is contained in:
@ -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,10 +34,8 @@ 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};
|
||||
PgTable *vas = proc->platformdata.cr3;
|
||||
ElfAuxval aux = {0};
|
||||
|
||||
Elf64_Ehdr *elfhdr = (Elf64_Ehdr *)data;
|
||||
aux.entry = elfhdr->e_entry;
|
||||
@ -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
|
||||
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);
|
||||
|
||||
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
|
||||
range->pgflags = pgflags;
|
||||
|
||||
LL_APPEND(proc->vas, range);
|
||||
} break;
|
||||
@ -98,19 +91,17 @@ 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));
|
||||
proc->platformdata.kstack = sp;
|
||||
hal_memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe));
|
||||
|
||||
proc->platformdata.trapframe.ss = 0x10;
|
||||
proc->platformdata.trapframe.rsp = (uint64_t)VIRT(sp);
|
||||
proc->platformdata.trapframe.rflags = 0x202;
|
||||
proc->platformdata.trapframe.cs = 0x08;
|
||||
proc->platformdata.trapframe.rip = (uint64_t)ent;
|
||||
proc->platformdata.cr3 = hal_vmm_current_cr3();
|
||||
proc->state = PROC_READY;
|
||||
proc->pid = pids++;
|
||||
#endif
|
||||
proc->platformdata.trapframe.ss = 0x10;
|
||||
proc->platformdata.trapframe.rsp = (uint64_t)VIRT(sp);
|
||||
proc->platformdata.trapframe.rflags = 0x202;
|
||||
proc->platformdata.trapframe.cs = 0x08;
|
||||
proc->platformdata.trapframe.rip = (uint64_t)ent;
|
||||
proc->platformdata.cr3 = hal_vmm_current_cr3();
|
||||
proc->state = PROC_READY;
|
||||
proc->pid = pids++;
|
||||
|
||||
return proc;
|
||||
}
|
||||
@ -136,29 +127,27 @@ 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));
|
||||
proc->platformdata.kstack = sp;
|
||||
hal_memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe));
|
||||
|
||||
proc->platformdata.cr3 = hal_vmm_userproc_pml4(proc);
|
||||
uint32_t flags = HAL_PG_RW | HAL_PG_USER | HAL_PG_PRESENT;
|
||||
hal_vmm_map_range(VIRT(proc->platformdata.cr3), spbase, spbase, PROC_STACKSIZE, flags);
|
||||
VasRange *range = dlmalloc(sizeof(*range));
|
||||
range->virtstart = spbase;
|
||||
range->physstart = spbase;
|
||||
range->size = PROC_STACKSIZE;
|
||||
range->pgflags = flags;
|
||||
proc->platformdata.cr3 = hal_vmm_userproc_pml4(proc);
|
||||
uint32_t flags = HAL_PG_RW | HAL_PG_USER | HAL_PG_PRESENT;
|
||||
hal_vmm_map_range(VIRT(proc->platformdata.cr3), spbase, spbase, PROC_STACKSIZE, flags);
|
||||
VasRange *range = dlmalloc(sizeof(*range));
|
||||
range->virtstart = spbase;
|
||||
range->physstart = spbase;
|
||||
range->size = PROC_STACKSIZE;
|
||||
range->pgflags = flags;
|
||||
|
||||
LL_APPEND(proc->vas, range);
|
||||
LL_APPEND(proc->vas, range);
|
||||
|
||||
ElfAuxval aux = proc_load_elf_segs(proc, data);
|
||||
ElfAuxval aux = proc_load_elf_segs(proc, data);
|
||||
|
||||
proc->platformdata.trapframe.ss = 0x20 | 0x3;
|
||||
proc->platformdata.trapframe.rsp = (uint64_t)VIRT(sp);
|
||||
proc->platformdata.trapframe.rflags = 0x202;
|
||||
proc->platformdata.trapframe.cs = 0x18 | 0x3;
|
||||
proc->platformdata.trapframe.rip = aux.entry;
|
||||
#endif
|
||||
proc->platformdata.trapframe.ss = 0x20 | 0x3;
|
||||
proc->platformdata.trapframe.rsp = (uint64_t)VIRT(sp);
|
||||
proc->platformdata.trapframe.rflags = 0x202;
|
||||
proc->platformdata.trapframe.cs = 0x18 | 0x3;
|
||||
proc->platformdata.trapframe.rip = aux.entry;
|
||||
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
|
||||
pmm_free((uintptr_t)(zombie->platformdata.kstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
|
||||
|
||||
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
|
||||
hal_vmm_unmap_range(zombie->platformdata.cr3, tmp->virtstart, tmp->physstart, tmp->size);
|
||||
// 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
|
||||
pmm_free((uintptr_t)zombie->platformdata.cr3, 1);
|
||||
}
|
||||
dlfree(zombie);
|
||||
} else {
|
||||
@ -226,20 +209,16 @@ void proc_sched(void *cpustate) {
|
||||
proc_reaper();
|
||||
}
|
||||
|
||||
#if defined(__x86_64__)
|
||||
IntrStackFrame *frame = cpustate;
|
||||
IntrStackFrame *frame = cpustate;
|
||||
|
||||
PROCS.current->platformdata.trapframe = *frame;
|
||||
PROCS.current->platformdata.fsbase = hal_cpu_rdmsr(HAL_CPU_FSBASE);
|
||||
#endif
|
||||
PROCS.current->platformdata.trapframe = *frame;
|
||||
PROCS.current->platformdata.fsbase = hal_cpu_rdmsr(HAL_CPU_FSBASE);
|
||||
|
||||
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
|
||||
hal_cpu_wrmsr(HAL_CPU_FSBASE, PROCS.current->platformdata.fsbase);
|
||||
hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3);
|
||||
}
|
||||
|
||||
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
|
||||
hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3);
|
||||
}
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user