diff --git a/kernel/include/thread/Scheduler.h b/kernel/include/thread/Scheduler.h index 3ccf8912..9621cf47 100644 --- a/kernel/include/thread/Scheduler.h +++ b/kernel/include/thread/Scheduler.h @@ -22,4 +22,6 @@ namespace Scheduler void reap_task(Task* task); void reap_tasks(); + + void reset_task(Task* task, ELFImage* new_image); } \ No newline at end of file diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index b3545427..233b9b7c 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -152,6 +152,25 @@ void Scheduler::load_user_task(const char* filename) new_task->id, new_task->regs.rsp, task_num); } +void Scheduler::reset_task(Task* task, ELFImage* new_image) +{ + task->state = task->Running; + task->regs.rip = new_image->entry; + task->image = new_image; + task->allocated_stack = (uint64_t)MemoryManager::get_pages( + TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? + task->regs.rsp = Utilities::get_top_of_stack(task->allocated_stack, TASK_PAGES_IN_STACK); + task->regs.cs = 0x18 | 0x03; + task->regs.ss = 0x20 | 0x03; + task->regs.ds = 0x20 | 0x03; + task->regs.rflags = (1 << 21) | (1 << 9); // enable interrupts + task->task_sleep = 0; + task->task_time = 0; + task->cpu_time = 0; + kinfoln("Resetting task: loaded at %lx, tid %ld, stack at %lx, total tasks: %ld", task->regs.rip, task->id, + task->regs.rsp, task_num); +} + void Scheduler::reap_task(Task* task) { ASSERT(!Interrupts::is_in_handler());