OnDemand paging for heap

This commit is contained in:
Mathieu Maret 2022-09-03 23:32:31 +02:00
parent 535290cf40
commit edcac85b35
3 changed files with 34 additions and 11 deletions

View File

@ -4,7 +4,10 @@
#include "interrupt.h"
#include "irq.h"
#include "klibc.h"
#include "process.h"
#include "thread.h"
#include "types.h"
#include "uaddrspace.h"
#include "vga.h"
exception_handler exception_handler_array[EXCEPTION_NUM] = {
@ -53,8 +56,15 @@ void pagefault_handler(struct cpu_state *frame, ulong intr)
{
struct thread *current = getCurrentThread();
printf("page fault while in thread %s code at 0x%x when trying to access 0x%x err_code 0x%x\n", current->name,
cpu_context_get_PC(frame), cpu_context_get_EX_faulting_vaddr(frame), cpu_context_get_EX_err(frame));
struct uAddrSpace *as = processGetAddrSpace(current->process);
vaddr_t faultAddr = cpu_context_get_EX_faulting_vaddr(frame);
if(!uAddrSpaceCheckNAlloc(as, faultAddr))
return;
printf("page fault while in thread [%s] at 0x%x when trying to access 0x%x err_code 0x%x\n", current->name,
cpu_context_get_PC(frame), faultAddr, cpu_context_get_EX_err(frame));
if (cpu_context_is_in_user_mode(frame)) {
printf("Killing User Thread\n");
threadExit();

View File

@ -1,6 +1,7 @@
#include "uaddrspace.h"
#include "alloc.h"
#include "kernel.h"
#include "klibc.h"
#include "mem.h"
#include "mmuContext.h"
#include "process.h"
@ -105,15 +106,26 @@ uaddr_t sysBrk(struct uAddrSpace *as, uaddr_t newHeapTop)
return as->heapStart + as->heapSize;
}
//WIP do it manually
for (uaddr_t begin = ALIGN(as->heapStart + as->heapSize, PAGE_SIZE); begin < newHeapTop; begin += PAGE_SIZE) {
paddr_t ppage = allocPhyPage(1);
if (0 != pageMap(begin, ppage,
PAGING_MEM_USER | PAGING_MEM_WRITE | PAGING_MEM_READ))
return (uaddr_t)NULL;
unrefPhyPage(ppage);
}
as->heapSize += incSize;
return 0;
}
int uAddrSpaceCheckNAlloc(struct uAddrSpace *as, vaddr_t addr)
{
pr_devel("Checking %p inside %p and %p", addr, as->heapStart, as->heapStart +as->heapSize);
if (addr < as->heapStart || addr >= as->heapStart + as->heapSize) {
return -1;
}
vaddr_t addrAlign = ALIGN_DOWN(addr, PAGE_SIZE);
paddr_t ppage = allocPhyPage(1);
if (0 != pageMap(addrAlign, ppage, PAGING_MEM_USER | PAGING_MEM_WRITE | PAGING_MEM_READ)) {
return -1;
}
unrefPhyPage(ppage);
return 0;
}

View File

@ -11,4 +11,5 @@ struct uAddrSpace * uAddrSpaceCreate(struct process *proc);
int uAddrSpaceDelete(struct uAddrSpace *addr);
struct mmu_context * uAddrSpaceGetMMUContext(struct uAddrSpace *addr);
int uAddrSpaceSetHeap(struct uAddrSpace *as, uaddr_t addr, size_t size);
int uAddrSpaceCheckNAlloc(struct uAddrSpace *as, vaddr_t addr);
uaddr_t sysBrk(struct uAddrSpace *as, uaddr_t newHeapTop);