Move userspace task to userspace memory
This commit is contained in:
parent
b334e1cd50
commit
966fdc76d7
@ -50,20 +50,9 @@ void sys_exec(Context* context, const char* pathname)
|
|||||||
return;
|
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())
|
if ((uint64_t)memusage > PMM::get_free())
|
||||||
{
|
{
|
||||||
kfree(kpathname);
|
kfree(kpathname);
|
||||||
MemoryManager::release_pages((void*)allocated_stack, TASK_PAGES_IN_STACK);
|
|
||||||
context->rax = -ENOMEM;
|
context->rax = -ENOMEM;
|
||||||
return;
|
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
|
ASSERT(image); // If check_elf_image succeeded, load_elf_from_vfs MUST succeed, unless something has gone terribly
|
||||||
// wrong.
|
// 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);
|
Scheduler::reset_task(task, image);
|
||||||
|
|
||||||
task->restore_context(context);
|
task->restore_context(context);
|
||||||
|
@ -112,8 +112,8 @@ Task* Scheduler::create_user_task()
|
|||||||
memset(&new_task->regs, 0, sizeof(Context));
|
memset(&new_task->regs, 0, sizeof(Context));
|
||||||
new_task->user_task = true;
|
new_task->user_task = true;
|
||||||
new_task->id = free_tid++;
|
new_task->id = free_tid++;
|
||||||
new_task->allocated_stack = (uint64_t)MemoryManager::get_pages(
|
new_task->allocated_stack = (uint64_t)MemoryManager::get_pages_at(
|
||||||
TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right?
|
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->regs.rsp = get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK);
|
||||||
new_task->task_sleep = 0;
|
new_task->task_sleep = 0;
|
||||||
new_task->task_time = 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->user_task = true;
|
||||||
new_task->regs.rip = image->entry;
|
new_task->regs.rip = image->entry;
|
||||||
new_task->image = image;
|
new_task->image = image;
|
||||||
new_task->allocated_stack = (uint64_t)MemoryManager::get_pages(
|
new_task->allocated_stack = (uint64_t)MemoryManager::get_pages_at(
|
||||||
TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right?
|
0x100000, TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right?
|
||||||
if (!new_task->allocated_stack)
|
if (!new_task->allocated_stack)
|
||||||
{
|
{
|
||||||
delete new_task;
|
delete new_task;
|
||||||
ELFLoader::release_elf_image(image);
|
ELFLoader::release_elf_image(image);
|
||||||
|
VMM::switch_back_to_kernel_address_space();
|
||||||
|
Interrupts::pop();
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
new_task->regs.rsp = get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK);
|
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);
|
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);
|
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);
|
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.
|
if (exiting_task->image) // FIXME: Also free pages the task has mmap-ed but not munmap-ed.
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user