80 lines
1.6 KiB
C
80 lines
1.6 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;
|
||
|
}
|