From b0ec767295731e1d9451ee383f52bf047e059a51 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Thu, 8 Feb 2024 00:07:12 +0100 Subject: [PATCH] init: use zero driver for stack alloc --- core/elf.c | 4 +--- core/main.c | 37 +++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/core/elf.c b/core/elf.c index f87572d..7837143 100644 --- a/core/elf.c +++ b/core/elf.c @@ -13,13 +13,11 @@ * @return 0 when the program is not a valid ELF */ -uaddr_t loadElfProg(const char *prog, struct process * proc) +uaddr_t loadElfProg(const char *prog, struct process *proc) { int i; uaddr_t lastUserAddr = 0; - - /* e_ident value */ #define ELFMAG0 0x7f #define ELFMAG1 'E' diff --git a/core/main.c b/core/main.c index 8bed568..2db8bec 100644 --- a/core/main.c +++ b/core/main.c @@ -84,27 +84,28 @@ void loadUserSpace() return; } - struct process *proc = processCreate("init"); + { + struct process *proc = processCreate("init"); + struct uAddrSpace *as = processGetAddrSpace(proc); - threadChangeCurrentContext(processGetMMUContext(proc)); - uaddr_t prog = loadElfProg(buf + FILE_HEADER_SIZE, proc); - if (prog == (uaddr_t)NULL) { - free(buf); - return; + threadChangeCurrentContext(processGetMMUContext(proc)); + uaddr_t prog = loadElfProg(buf + FILE_HEADER_SIZE, proc); + if (prog == (uaddr_t)NULL) { + free(buf); + return; + } + + // Alloc user stack + size_t stackSize = PAGE_SIZE * 2; + uaddr_t stack = PAGING_TOP_USER_ADDRESS - stackSize + 1; + zeroMmap(as, &stack, stackSize, PAGING_MEM_USER | PAGING_MEM_WRITE | PAGING_MEM_READ, + 0); + + threadCreateUser("init", proc, prog, 0, 0, stack + stackSize - 4); + processUnref(proc); + threadChangeCurrentContext(NULL); } - // Alloc user stack - uaddr_t stackTop = 0xfffffffc; - uaddr_t stackBottom = ALIGN_DOWN(stackTop, PAGE_SIZE); - paddr_t stackPhy = allocPhyPage(1); - assert(pageMap(stackBottom, stackPhy, - PAGING_MEM_USER | PAGING_MEM_WRITE | PAGING_MEM_READ) == 0); - unrefPhyPage(stackPhy); - - threadCreateUser("init", proc, prog, 0, 0, stackTop); - processUnref(proc); - threadChangeCurrentContext(NULL); - free(buf); }