diff --git a/kernel/src/sys/exec.cpp b/kernel/src/sys/exec.cpp index 9db03d95..76eb528f 100644 --- a/kernel/src/sys/exec.cpp +++ b/kernel/src/sys/exec.cpp @@ -50,20 +50,9 @@ void sys_exec(Context* context, const char* pathname) return; } - uint64_t allocated_stack = (uint64_t)MemoryManager::get_pages(TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); - if (!allocated_stack) - { - kfree(kpathname); - context->rax = -ENOMEM; - return; - } - - uint64_t allocated_stack_phys = VMM::get_physical(allocated_stack); - if ((uint64_t)memusage > PMM::get_free()) { kfree(kpathname); - MemoryManager::release_pages((void*)allocated_stack, TASK_PAGES_IN_STACK); context->rax = -ENOMEM; return; } @@ -80,13 +69,6 @@ void sys_exec(Context* context, const char* pathname) ASSERT(image); // If check_elf_image succeeded, load_elf_from_vfs MUST succeed, unless something has gone terribly // wrong. - task->allocated_stack = allocated_stack; - - for (uint64_t i = 0; i < TASK_PAGES_IN_STACK; i++) - { - VMM::map(allocated_stack + (i * PAGE_SIZE), allocated_stack_phys + (i * PAGE_SIZE), MAP_READ_WRITE | MAP_USER); - } - Scheduler::reset_task(task, image); task->restore_context(context); diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index b9cc852a..efcd22a2 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -112,8 +112,8 @@ Task* Scheduler::create_user_task() memset(&new_task->regs, 0, sizeof(Context)); new_task->user_task = true; new_task->id = free_tid++; - new_task->allocated_stack = (uint64_t)MemoryManager::get_pages( - TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? + new_task->allocated_stack = (uint64_t)MemoryManager::get_pages_at( + 0x100000, TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? new_task->regs.rsp = get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); new_task->task_sleep = 0; new_task->task_time = 0; @@ -148,12 +148,14 @@ long Scheduler::load_user_task(const char* filename) new_task->user_task = true; new_task->regs.rip = image->entry; new_task->image = image; - new_task->allocated_stack = (uint64_t)MemoryManager::get_pages( - TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? + new_task->allocated_stack = (uint64_t)MemoryManager::get_pages_at( + 0x100000, TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? if (!new_task->allocated_stack) { delete new_task; ELFLoader::release_elf_image(image); + VMM::switch_back_to_kernel_address_space(); + Interrupts::pop(); return -ENOMEM; } new_task->regs.rsp = get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); @@ -204,7 +206,7 @@ void Scheduler::reap_task(Task* task) VMM::switch_to_user_address_space(exiting_task->address_space); } kinfoln("reaping task %ld, exited with code %ld", exiting_task->id, exiting_task->exit_status); - if (exiting_task->allocated_stack) + if (exiting_task->allocated_stack && !exiting_task->is_user_task()) MemoryManager::release_pages((void*)exiting_task->allocated_stack, TASK_PAGES_IN_STACK); if (exiting_task->image) // FIXME: Also free pages the task has mmap-ed but not munmap-ed. {