From a6f0a7056fa743001d12710dada57f38367fa062 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 12 Oct 2022 17:07:39 +0200 Subject: [PATCH] Scheduler: Set the user_task field in a Task at creation time We were previously looking at its segment registers to see if they were user-like, but this method is bad. What is the task was executing a system call? So now, we store that value at creation time. --- kernel/include/thread/Task.h | 2 ++ kernel/src/thread/Scheduler.cpp | 5 +++++ kernel/src/thread/Task.cpp | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/kernel/include/thread/Task.h b/kernel/include/thread/Task.h index 59239d24..77551e5a 100644 --- a/kernel/include/thread/Task.h +++ b/kernel/include/thread/Task.h @@ -36,6 +36,8 @@ struct Task char floating_region[512] __attribute__((aligned(16))); bool floating_saved = false; + bool user_task = true; + bool is_user_task(); ELFImage* image = nullptr; diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index efadec98..fe8cef85 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -41,6 +41,7 @@ void Scheduler::init() idle_task.regs.ss = 0x10; idle_task.regs.rflags = (1 << 21) | (1 << 9); idle_task.task_sleep = 1000; + idle_task.user_task = false; idle_task.state = idle_task.Idle; base_task = new Task; @@ -51,6 +52,7 @@ void Scheduler::init() sched_current_task->next_task = sched_current_task; sched_current_task->prev_task = sched_current_task; sched_current_task->state = sched_current_task->Running; + sched_current_task->user_task = false; task_num++; // the other registers will be saved next task switch @@ -61,6 +63,7 @@ void Scheduler::add_kernel_task(void (*task)(void)) { Task* new_task = new Task; ASSERT(new_task); + new_task->user_task = false; new_task->id = free_tid++; new_task->regs.rip = (uint64_t)task; new_task->allocated_stack = @@ -88,6 +91,7 @@ void Scheduler::add_user_task(void* task) { Task* new_task = new Task; ASSERT(new_task); + new_task->user_task = true; new_task->id = free_tid++; new_task->regs.rip = (uint64_t)task; new_task->allocated_stack = (uint64_t)MemoryManager::get_pages( @@ -124,6 +128,7 @@ void Scheduler::load_user_task(const char* filename) delete new_task; return; } + new_task->user_task = true; new_task->regs.rip = image->entry; new_task->image = image; new_task->allocated_stack = (uint64_t)MemoryManager::get_pages( diff --git a/kernel/src/thread/Task.cpp b/kernel/src/thread/Task.cpp index 17226a60..0c924d91 100644 --- a/kernel/src/thread/Task.cpp +++ b/kernel/src/thread/Task.cpp @@ -25,5 +25,5 @@ void task_restore_floating(Task& task) bool Task::is_user_task() { - return (regs.cs & 3) > 0; + return user_task; } \ No newline at end of file