Wip: zero mmap

This commit is contained in:
Mathieu Maret 2023-11-10 09:01:55 +01:00
parent 20ef80f754
commit 4ed181198c
7 changed files with 101 additions and 29 deletions

View File

@ -17,6 +17,7 @@
#define PAGING_MEM_USER (1U << 0) #define PAGING_MEM_USER (1U << 0)
#define PAGING_MEM_READ (1U << 1) #define PAGING_MEM_READ (1U << 1)
#define PAGING_MEM_WRITE (1U << 2) #define PAGING_MEM_WRITE (1U << 2)
#define PAGING_MEM_EXEC (1U << 3)
int pagingSetup(paddr_t lowerKernelAddr, paddr_t upperKernelAddr); int pagingSetup(paddr_t lowerKernelAddr, paddr_t upperKernelAddr);

View File

@ -84,7 +84,7 @@ void loadUserSpace()
return; return;
} }
struct process *proc = processCreate("UserSpace"); struct process *proc = processCreate("init");
threadChangeCurrentContext(processGetMMUContext(proc)); threadChangeCurrentContext(processGetMMUContext(proc));
uaddr_t prog = loadElfProg(buf + FILE_HEADER_SIZE, 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); PAGING_MEM_USER | PAGING_MEM_WRITE | PAGING_MEM_READ) == 0);
unrefPhyPage(stackPhy); unrefPhyPage(stackPhy);
threadCreateUser("UserProg", proc, prog, 0, 0, stackTop); threadCreateUser("init", proc, prog, 0, 0, stackTop);
processUnref(proc); processUnref(proc);
threadChangeCurrentContext(NULL); threadChangeCurrentContext(NULL);

View File

@ -7,6 +7,8 @@
#include "types.h" #include "types.h"
#include "uaccess.h" #include "uaccess.h"
#include "uaddrspace.h" #include "uaddrspace.h"
#include "zero.h"
int syscallExecute(int syscallId, const struct cpu_state *userCtx) 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); (unsigned int *)&flags, (unsigned int *)&userPath);
strzcpyFromUser((vaddr_t *)path, (uaddr_t *)userPath, sizeof(path)); strzcpyFromUser((vaddr_t *)path, (uaddr_t *)userPath, sizeof(path));
printf("Trying mmap for device %s\n", path); printf("Trying mmap for device %s\n", path);
if(strcmp(path, "/dev/zero") == 0){
zeroMmap(as, &uaddr, size, rights,flags);
}
(void)as; (void)as;
break; break;
} }

View File

@ -14,18 +14,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
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 uAddrSpace {
struct process *process; // The process that is represented by this AS struct process *process; // The process that is represented by this AS
struct mmu_context *ctx; // The corresponding MMU configuration 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 struct uAddrVirtualReg *listVirtualReg; // List of Virtual Region used by this process
uaddr_t heapStart; // Start of the Head 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) struct uAddrSpace *uAddrSpaceCreate(struct process *proc)
{ {
@ -161,3 +136,15 @@ free_ppage:
unrefPhyPage(ppage); unrefPhyPage(ppage);
return -1; 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;
}

View File

@ -7,9 +7,41 @@
struct uAddrSpace; struct uAddrSpace;
struct uAddrVirtualReg; 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); struct uAddrSpace * uAddrSpaceCreate(struct process *proc);
int uAddrSpaceDelete(struct uAddrSpace *addr); int uAddrSpaceDelete(struct uAddrSpace *addr);
struct mmu_context * uAddrSpaceGetMMUContext(struct uAddrSpace *addr); struct mmu_context * uAddrSpaceGetMMUContext(struct uAddrSpace *addr);
int uAddrSpaceSetHeap(struct uAddrSpace *as, uaddr_t addr, size_t size); int uAddrSpaceSetHeap(struct uAddrSpace *as, uaddr_t addr, size_t size);
int uAddrSpaceCheckNAlloc(struct uAddrSpace *as, vaddr_t addr); int uAddrSpaceCheckNAlloc(struct uAddrSpace *as, vaddr_t addr);
uaddr_t sysBrk(struct uAddrSpace *as, uaddr_t newHeapTop); 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);

45
drivers/zero.c Normal file
View File

@ -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;
}

View File

@ -2,8 +2,10 @@
#include "types.h" #include "types.h"
#include "uaddrspace.h"
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
int zeroSetup(); 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);