matos/arch/x86/mmuContext.c
2021-10-28 00:41:02 +02:00

101 lines
2.0 KiB
C

#include "mmuContext.h"
#include "alloc.h"
#include "allocArea.h"
#include "errno.h"
#include "klibc.h"
#include "irq.h"
#include "list.h"
#include "paging.h"
#include "stdarg.h"
#include "types.h"
struct mmu_context {
paddr_t paddr_PD;
vaddr_t vaddr_PD;
uint32_t ref;
struct mmu_context *next, *prev;
};
static struct mmu_context *listContext = NULL;
static struct mmu_context *currentContext = NULL;
int mmuContextSetup()
{
struct mmu_context *initialCtx;
int ret = 0;
initialCtx = malloc(sizeof(struct mmu_context));
if (initialCtx == NULL)
return -ENOMEM;
initialCtx->paddr_PD = pagingGetCurrentPDPaddr();
initialCtx->vaddr_PD = areaAlloc(1, 0);
ret = pageMap(initialCtx->vaddr_PD, initialCtx->paddr_PD,
PAGING_MEM_WRITE | PAGING_MEM_READ);
if (ret)
return ret;
list_singleton(listContext, initialCtx);
currentContext = initialCtx;
// We create the context and we are using it
initialCtx->ref = 2;
return 0;
}
struct mmu_context *mmuContextCreate()
{
struct mmu_context *ctx;
uint32_t flags;
ctx = malloc(sizeof(struct mmu_context));
if (ctx == NULL)
return NULL;
ctx->vaddr_PD = areaAlloc(1, AREA_PHY_MAP);
if (ctx->vaddr_PD == (vaddr_t)NULL) {
pr_info("Fail to allocate MMU Context\n");
free(ctx);
return NULL;
}
ctx->paddr_PD = pagingGetPaddr(ctx->vaddr_PD);
ctx->ref = 1;
//TODO copy kernel space
disable_IRQs(flags);
list_add_tail(listContext, ctx);
restore_IRQs(flags);
return ctx;
}
int mmuContextSyncKernelPDE(int pdEntry, void * pde, size_t pdeSize)
{
uint32_t flags;
struct mmu_context *destContext;
int nbContexts;
disable_IRQs(flags);
list_foreach_forward(listContext, destContext, nbContexts)
{
void *dest_pd;
assert(destContext->ref > 0);
dest_pd = (void *)destContext->vaddr_PD;
memcpy(dest_pd + pdEntry *pdeSize, pde, pdeSize);
}
restore_IRQs(flags);
return 0;
}