From 4ed181198c078b51a367f01842140ecc3daa986f Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 10 Nov 2023 09:01:55 +0100 Subject: [PATCH] Wip: zero mmap --- arch/x86/paging.h | 1 + core/main.c | 4 ++-- core/syscall.c | 5 +++++ core/uaddrspace.c | 39 +++++++++++++-------------------------- core/uaddrspace.h | 32 ++++++++++++++++++++++++++++++++ drivers/zero.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ drivers/zero.h | 4 +++- 7 files changed, 101 insertions(+), 29 deletions(-) create mode 100644 drivers/zero.c diff --git a/arch/x86/paging.h b/arch/x86/paging.h index 360d052..e18d1f0 100644 --- a/arch/x86/paging.h +++ b/arch/x86/paging.h @@ -17,6 +17,7 @@ #define PAGING_MEM_USER (1U << 0) #define PAGING_MEM_READ (1U << 1) #define PAGING_MEM_WRITE (1U << 2) +#define PAGING_MEM_EXEC (1U << 3) int pagingSetup(paddr_t lowerKernelAddr, paddr_t upperKernelAddr); diff --git a/core/main.c b/core/main.c index ba1f9ef..8bed568 100644 --- a/core/main.c +++ b/core/main.c @@ -84,7 +84,7 @@ void loadUserSpace() return; } - struct process *proc = processCreate("UserSpace"); + struct process *proc = processCreate("init"); threadChangeCurrentContext(processGetMMUContext(proc)); uaddr_t prog = loadElfProg(buf + FILE_HEADER_SIZE, proc); @@ -101,7 +101,7 @@ void loadUserSpace() PAGING_MEM_USER | PAGING_MEM_WRITE | PAGING_MEM_READ) == 0); unrefPhyPage(stackPhy); - threadCreateUser("UserProg", proc, prog, 0, 0, stackTop); + threadCreateUser("init", proc, prog, 0, 0, stackTop); processUnref(proc); threadChangeCurrentContext(NULL); diff --git a/core/syscall.c b/core/syscall.c index 8fac05a..5ebfc55 100644 --- a/core/syscall.c +++ b/core/syscall.c @@ -7,6 +7,8 @@ #include "types.h" #include "uaccess.h" #include "uaddrspace.h" +#include "zero.h" + int syscallExecute(int syscallId, const struct cpu_state *userCtx) { @@ -71,6 +73,9 @@ int syscallExecute(int syscallId, const struct cpu_state *userCtx) (unsigned int *)&flags, (unsigned int *)&userPath); strzcpyFromUser((vaddr_t *)path, (uaddr_t *)userPath, sizeof(path)); printf("Trying mmap for device %s\n", path); + if(strcmp(path, "/dev/zero") == 0){ + zeroMmap(as, &uaddr, size, rights,flags); + } (void)as; break; } diff --git a/core/uaddrspace.c b/core/uaddrspace.c index 2ee21e1..f621541 100644 --- a/core/uaddrspace.c +++ b/core/uaddrspace.c @@ -14,18 +14,6 @@ #include #include -struct uAddrVirtualReg { - uaddr_t addr; - size_t size; - int right; // PAGING_MEM_* - uint32_t offset; // in the mappedRessource - uint flags; - - struct mappedRessource *res; - struct uAddrVirtualReg *nextInAddrSpace, *prevInAddrSpace; - struct uAddrVirtualReg *nextInMappedRes, *prevInMappedRes; -}; - struct uAddrSpace { struct process *process; // The process that is represented by this AS struct mmu_context *ctx; // The corresponding MMU configuration @@ -33,22 +21,9 @@ struct uAddrSpace { struct uAddrVirtualReg *listVirtualReg; // List of Virtual Region used by this process uaddr_t heapStart; // Start of the Head - size_t heapSize; // Hep size -> modified by brk() + size_t heapSize; // Heap size -> modified by brk() }; -struct mappedRessourceOps { - int (*open)(struct uAddrVirtualReg *vreg); - int (*close)(struct uAddrVirtualReg *vreg); - int (*unmap)(struct uAddrVirtualReg *vregi, uaddr_t addr, size_t size); - int (*nopage)(struct uAddrVirtualReg *vregi, uaddr_t addr, - int right); // Called by the pageflt handler when the page is missing -}; - -struct mappedRessource { - int right; // PAGING_MEM_* - struct mappedRessourceOps *ops; - struct uAddrVirtualReg *listVirtualReg; -}; struct uAddrSpace *uAddrSpaceCreate(struct process *proc) { @@ -161,3 +136,15 @@ free_ppage: unrefPhyPage(ppage); return -1; } + +int uAddrSpaceMmap(struct uAddrSpace *as, uaddr_t *uaddr, size_t size, uint32_t rights, uint32_t flags, + struct mappedRessource *res) +{ + (void)as; + (void)uaddr; + (void)rights; + (void)size; + (void)flags; + (void)res; + return 0; +} diff --git a/core/uaddrspace.h b/core/uaddrspace.h index 37938de..8fa3e53 100644 --- a/core/uaddrspace.h +++ b/core/uaddrspace.h @@ -7,9 +7,41 @@ struct uAddrSpace; struct uAddrVirtualReg; +// TODO : move this struct to .c and add accessors +struct uAddrVirtualReg { + uaddr_t addr; + size_t size; + int right; // PAGING_MEM_* + uint32_t offset; // in the mappedRessource + uint flags; + + struct mappedRessource *res; + struct uAddrVirtualReg *nextInAddrSpace, *prevInAddrSpace; + struct uAddrVirtualReg *nextInMappedRes, *prevInMappedRes; +}; + +struct mappedRessourceOps { + int (*open)(struct uAddrVirtualReg *vreg); + int (*close)(struct uAddrVirtualReg *vreg); + int (*unmap)(struct uAddrVirtualReg *vreg, uaddr_t addr, size_t size); + int (*nopage)(struct uAddrVirtualReg *vreg, uaddr_t addr, + int right); // Called by the pageflt handler when the page is missing +}; + +struct mappedRessource { + int allowedRight; // PAGING_MEM_* + struct mappedRessourceOps *ops; + struct uAddrVirtualReg *listVirtualReg; + void *customData; +}; + + 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); + +int uAddrSpaceMmap(struct uAddrSpace *as, uaddr_t *uaddr, size_t size, uint32_t rights, uint32_t flags, + struct mappedRessource *res); diff --git a/drivers/zero.c b/drivers/zero.c new file mode 100644 index 0000000..0580a65 --- /dev/null +++ b/drivers/zero.c @@ -0,0 +1,45 @@ +#include "zero.h" +#include "alloc.h" + +struct zeroMappedEntry { + int refCnt; +}; + +static int zeroOpen(struct uAddrVirtualReg *vreg) +{ + (void)vreg; + return 0; +} +static int zeroClose(struct uAddrVirtualReg *vreg) +{ + (void)vreg; + return 0; +} +static int zeroNoPage(struct uAddrVirtualReg *vreg, uaddr_t addr, int right) +{ + (void)vreg; + (void)addr; + (void)right; + return 0; +} + +static struct mappedRessourceOps zeroOps = { + .open = zeroOpen, + .close = zeroClose, + .unmap = NULL, + .nopage = zeroNoPage, +}; + +int zeroMmap(struct uAddrSpace *as, uaddr_t *uaddr, size_t size, uint32_t rights, uint32_t flags) +{ + struct mappedRessource *res = (struct mappedRessource *)zalloc(sizeof(struct mappedRessource)); + struct zeroMappedEntry *cust = (struct zeroMappedEntry *)zalloc(sizeof(struct zeroMappedEntry)); + + res->allowedRight = PAGING_MEM_READ | PAGING_MEM_WRITE | PAGING_MEM_EXEC | PAGING_MEM_USER; + res->ops = &zeroOps; + res->customData = cust; + + uAddrSpaceMmap(as, uaddr, size, rights, flags, res); + + return 0; +} diff --git a/drivers/zero.h b/drivers/zero.h index 83f4672..3fbd6fd 100644 --- a/drivers/zero.h +++ b/drivers/zero.h @@ -2,8 +2,10 @@ #include "types.h" +#include "uaddrspace.h" + #include #include int zeroSetup(); -int zeroMmap(uaddr_t *uaddr, size_t size, uint32_t rights, uint32_t flags); +int zeroMmap(struct uAddrSpace *as, uaddr_t *uaddr, size_t size, uint32_t rights, uint32_t flags);