mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-11-01 15:08:21 +00:00
vmm: page fault handling
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
#include <kernel/pmm.h>
|
#include <kernel/pmm.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <logging.h>
|
#include <logging.h>
|
||||||
|
#include <kernel/interrupt.h>
|
||||||
|
#include <kernel/vmm.h>
|
||||||
|
|
||||||
#define ADDRMASK (uint64_t)0x7ffffffffffff000
|
#define ADDRMASK (uint64_t)0x7ffffffffffff000
|
||||||
#define PTMASK (uint64_t)0b111111111000000000000
|
#define PTMASK (uint64_t)0b111111111000000000000
|
||||||
@@ -186,6 +188,24 @@ static volatile struct limine_kernel_address_request kaddrreq = {
|
|||||||
.revision = 0
|
.revision = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define ERROR_PRESENT 1
|
||||||
|
#define ERROR_WRITE 2
|
||||||
|
#define ERROR_FETCH 16
|
||||||
|
|
||||||
|
static void pfisr(isr_t *self, context_t *ctx) {
|
||||||
|
int vmmerror = 0;
|
||||||
|
if (ctx->error & ERROR_PRESENT)
|
||||||
|
vmmerror |= VMM_ACTION_READ;
|
||||||
|
|
||||||
|
if (ctx->error & ERROR_WRITE)
|
||||||
|
vmmerror |= VMM_ACTION_WRITE;
|
||||||
|
|
||||||
|
if (ctx->error & ERROR_FETCH)
|
||||||
|
vmmerror |= VMM_ACTION_EXEC;
|
||||||
|
|
||||||
|
__assert(vmm_pagefault((void *)ctx->cr2, ctx->cs == 8, vmmerror));
|
||||||
|
}
|
||||||
|
|
||||||
void arch_mmu_init() {
|
void arch_mmu_init() {
|
||||||
template = pmm_alloc(1, PMM_SECTION_DEFAULT);
|
template = pmm_alloc(1, PMM_SECTION_DEFAULT);
|
||||||
__assert(template);
|
__assert(template);
|
||||||
@@ -226,4 +246,5 @@ void arch_mmu_init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
arch_mmu_switch(FROM_HHDM(template));
|
arch_mmu_switch(FROM_HHDM(template));
|
||||||
|
interrupt_register(14, pfisr, NULL, 0); // FIXME still haven't decided if priorities grow up or down. should be max priority
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,10 @@
|
|||||||
|
|
||||||
#define VMM_PERMANENT_FLAGS_MASK (VMM_FLAGS_FILE | VMM_FLAGS_SHARED | VMM_FLAGS_COPY)
|
#define VMM_PERMANENT_FLAGS_MASK (VMM_FLAGS_FILE | VMM_FLAGS_SHARED | VMM_FLAGS_COPY)
|
||||||
|
|
||||||
|
#define VMM_ACTION_READ 1
|
||||||
|
#define VMM_ACTION_WRITE 2
|
||||||
|
#define VMM_ACTION_EXEC 4
|
||||||
|
|
||||||
struct vmmcache_t;
|
struct vmmcache_t;
|
||||||
typedef struct vmmrange_t{
|
typedef struct vmmrange_t{
|
||||||
struct vmmrange_t *next;
|
struct vmmrange_t *next;
|
||||||
@@ -51,6 +55,7 @@ typedef struct {
|
|||||||
|
|
||||||
void *vmm_map(void *addr, size_t size, int flags, mmuflags_t mmuflags, void *private);
|
void *vmm_map(void *addr, size_t size, int flags, mmuflags_t mmuflags, void *private);
|
||||||
void vmm_unmap(void *addr, size_t size, int flags);
|
void vmm_unmap(void *addr, size_t size, int flags);
|
||||||
|
bool vmm_pagefault(void *addr, bool user, int actions);
|
||||||
vmmcontext_t *vmm_newcontext();
|
vmmcontext_t *vmm_newcontext();
|
||||||
void vmm_init();
|
void vmm_init();
|
||||||
|
|
||||||
|
|||||||
+52
-1
@@ -4,6 +4,7 @@
|
|||||||
#include <util.h>
|
#include <util.h>
|
||||||
#include <arch/cpu.h>
|
#include <arch/cpu.h>
|
||||||
#include <limine.h>
|
#include <limine.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define RANGE_TOP(x) (void *)((uintptr_t)x->start + x->size)
|
#define RANGE_TOP(x) (void *)((uintptr_t)x->start + x->size)
|
||||||
|
|
||||||
@@ -161,6 +162,55 @@ static void insertrange(vmmspace_t *space, vmmrange_t *newrange) {
|
|||||||
newrange->next = NULL;
|
newrange->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool vmm_pagefault(void *addr, bool user, int actions) {
|
||||||
|
if (user == false)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
addr = (void *)ROUND_DOWN((uintptr_t)addr, PAGE_SIZE);
|
||||||
|
|
||||||
|
vmmspace_t *space = getspace(addr);
|
||||||
|
|
||||||
|
if (space == NULL || (space == &kernelspace && user))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// TODO lock
|
||||||
|
vmmrange_t *range = getrange(space, addr);
|
||||||
|
|
||||||
|
bool status = false;
|
||||||
|
|
||||||
|
if (range == NULL)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
// check if valid
|
||||||
|
|
||||||
|
int invalidactions = 0;
|
||||||
|
|
||||||
|
if ((range->mmuflags & ARCH_MMU_FLAG_READ) == 0)
|
||||||
|
invalidactions |= VMM_ACTION_READ;
|
||||||
|
|
||||||
|
if ((range->mmuflags & ARCH_MMU_FLAG_WRITE) == 0)
|
||||||
|
invalidactions |= VMM_ACTION_WRITE;
|
||||||
|
|
||||||
|
if ((range->mmuflags & ARCH_MMU_FLAG_NOEXEC))
|
||||||
|
invalidactions |= VMM_ACTION_EXEC;
|
||||||
|
|
||||||
|
if (invalidactions & actions)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
// TODO file/CoW
|
||||||
|
|
||||||
|
void *paddr = pmm_alloc(1, PMM_SECTION_DEFAULT);
|
||||||
|
__assert(paddr); // XXX handle these better than an assert once there are signals and such
|
||||||
|
__assert(arch_mmu_map(_cpu()->vmmctx->pagetable, paddr, addr, range->mmuflags));
|
||||||
|
memset(addr, 0, PAGE_SIZE);
|
||||||
|
|
||||||
|
status = true;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
// TODO unlock
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
void *vmm_map(void *addr, volatile size_t size, int flags, mmuflags_t mmuflags, void *private) {
|
void *vmm_map(void *addr, volatile size_t size, int flags, mmuflags_t mmuflags, void *private) {
|
||||||
addr = (void *)ROUND_DOWN((uintptr_t)addr, PAGE_SIZE);
|
addr = (void *)ROUND_DOWN((uintptr_t)addr, PAGE_SIZE);
|
||||||
if (flags & VMM_FLAGS_PAGESIZE)
|
if (flags & VMM_FLAGS_PAGESIZE)
|
||||||
@@ -220,6 +270,7 @@ void *vmm_map(void *addr, volatile size_t size, int flags, mmuflags_t mmuflags,
|
|||||||
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
memset(MAKE_HHDM(allocated), 0, PAGE_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,7 +301,7 @@ static void printspace(vmmspace_t *space) {
|
|||||||
printf("vmm: ranges:\n");
|
printf("vmm: ranges:\n");
|
||||||
vmmrange_t *range = space->ranges;
|
vmmrange_t *range = space->ranges;
|
||||||
while (range) {
|
while (range) {
|
||||||
printf("vmm: address %p size %p flags %p\n", range->start, range->size, range->flags);
|
printf("vmm: address %p size %lx flags %x\n", range->start, range->size, range->flags);
|
||||||
range = range->next;
|
range = range->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user