Compare commits
2 Commits
b58186265e
...
de49dcc01a
Author | SHA1 | Date |
---|---|---|
Drew Galbraith | de49dcc01a | |
Drew Galbraith | 7fe6c24aa5 |
|
@ -14,6 +14,7 @@ add_executable(zion
|
|||
scheduler/context_switch.s
|
||||
scheduler/jump_user_space.s
|
||||
scheduler/process.cpp
|
||||
scheduler/process_manager.cpp
|
||||
scheduler/scheduler.cpp
|
||||
scheduler/thread.cpp
|
||||
syscall/syscall.cpp
|
||||
|
|
|
@ -123,7 +123,7 @@ extern "C" void interrupt_timer(InterruptFrame*) {
|
|||
dbgln("timer: %us", cnt * 50 / 1000);
|
||||
}
|
||||
outb(PIC1_COMMAND, PIC_EOI);
|
||||
sched::Preempt();
|
||||
gScheduler->Preempt();
|
||||
}
|
||||
|
||||
void EnablePic() {
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include "debug/debug.h"
|
||||
#include "loader/elf_loader.h"
|
||||
#include "scheduler/process.h"
|
||||
#include "scheduler/scheduler.h"
|
||||
#include "scheduler/process_manager.h"
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -25,6 +25,6 @@ const limine_file& GetInitProgram() {
|
|||
void LoadInitProgram() {
|
||||
const limine_file& init_prog = GetInitProgram();
|
||||
|
||||
sched::InsertProcess(
|
||||
gProcMan->InsertProcess(
|
||||
new Process(reinterpret_cast<uint64_t>(init_prog.address)));
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ Process::Process(uint64_t elf_ptr) : id_(gNextId++), state_(RUNNING) {
|
|||
void Process::CreateThread(uint64_t elf_ptr) {
|
||||
Thread* thread = new Thread(this, next_thread_id_++, elf_ptr);
|
||||
threads_.PushBack(thread);
|
||||
sched::EnqueueThread(thread);
|
||||
gScheduler->Enqueue(thread);
|
||||
}
|
||||
|
||||
SharedPtr<Thread> Process::GetThread(uint64_t tid) {
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
#include "scheduler/process_manager.h"
|
||||
|
||||
ProcessManager* gProcMan = nullptr;
|
||||
|
||||
void ProcessManager::Init() {
|
||||
gProcMan = new ProcessManager();
|
||||
gProcMan->InsertProcess(Process::RootProcess());
|
||||
}
|
||||
|
||||
void ProcessManager::InsertProcess(const SharedPtr<Process>& proc) {
|
||||
proc_list_.PushBack(proc);
|
||||
}
|
||||
|
||||
Process& ProcessManager::FromId(uint64_t pid) {
|
||||
auto iter = proc_list_.begin();
|
||||
while (iter != proc_list_.end()) {
|
||||
if (iter->id() == pid) {
|
||||
return **iter;
|
||||
}
|
||||
++iter;
|
||||
}
|
||||
|
||||
panic("Searching for invalid process id");
|
||||
return *((Process*)0);
|
||||
}
|
||||
|
||||
void ProcessManager::DumpProcessStates() {
|
||||
dbgln("Process States: %u", proc_list_.size());
|
||||
auto iter = proc_list_.begin();
|
||||
while (iter != proc_list_.end()) {
|
||||
dbgln("%u: %u", iter->id(), iter->GetState());
|
||||
++iter;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include "lib/linked_list.h"
|
||||
#include "lib/shared_ptr.h"
|
||||
#include "scheduler/process.h"
|
||||
|
||||
class ProcessManager {
|
||||
public:
|
||||
// Initializes the ProcessManager
|
||||
// and stores it in the global variable.
|
||||
static void Init();
|
||||
|
||||
void InsertProcess(const SharedPtr<Process>& proc);
|
||||
Process& FromId(uint64_t id);
|
||||
|
||||
void DumpProcessStates();
|
||||
|
||||
private:
|
||||
// TODO: This should be a hashmap.
|
||||
LinkedList<SharedPtr<Process>> proc_list_;
|
||||
};
|
||||
|
||||
extern ProcessManager* gProcMan;
|
|
@ -2,136 +2,86 @@
|
|||
|
||||
#include "debug/debug.h"
|
||||
#include "lib/linked_list.h"
|
||||
#include "scheduler/process_manager.h"
|
||||
|
||||
namespace sched {
|
||||
namespace {
|
||||
|
||||
extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp);
|
||||
|
||||
void DumpProcessStates(LinkedList<SharedPtr<Process>>& proc_list) {
|
||||
dbgln("Process States: %u", proc_list.size());
|
||||
auto iter = proc_list.begin();
|
||||
while (iter != proc_list.end()) {
|
||||
dbgln("%u: %u", iter->id(), iter->GetState());
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
|
||||
class Scheduler {
|
||||
public:
|
||||
Scheduler() {
|
||||
SharedPtr<Process> root = Process::RootProcess();
|
||||
sleep_thread_ = root->GetThread(0);
|
||||
// TODO: Implement a separate sleep thread?
|
||||
current_thread_ = sleep_thread_;
|
||||
proc_list_.PushBack(root);
|
||||
}
|
||||
void Enable() { enabled_ = true; }
|
||||
|
||||
Process& CurrentProcess() { return CurrentThread().process(); }
|
||||
Thread& CurrentThread() { return *current_thread_; }
|
||||
|
||||
void InsertProcess(Process* process) { proc_list_.PushBack(process); }
|
||||
void Enqueue(Thread* thread) { runnable_threads_.PushBack(thread); }
|
||||
|
||||
void SwapToCurrent(Thread& prev) {
|
||||
if (current_thread_->GetState() != Thread::RUNNABLE) {
|
||||
panic("Swapping to non-runnable thread.");
|
||||
}
|
||||
current_thread_->SetState(Thread::RUNNING);
|
||||
|
||||
context_switch(prev.Rsp0Ptr(), current_thread_->Rsp0Ptr());
|
||||
|
||||
asm volatile("sti");
|
||||
}
|
||||
|
||||
void Preempt() {
|
||||
if (!enabled_) {
|
||||
return;
|
||||
}
|
||||
|
||||
asm volatile("cli");
|
||||
if (current_thread_ == sleep_thread_) {
|
||||
// Sleep should never be preempted. (We should yield it if another thread
|
||||
// becomes scheduleable).
|
||||
return;
|
||||
}
|
||||
|
||||
if (runnable_threads_.size() == 0) {
|
||||
// Continue.
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPtr<Thread> prev = current_thread_;
|
||||
prev->SetState(Thread::RUNNABLE);
|
||||
current_thread_ = runnable_threads_.CycleFront(prev);
|
||||
|
||||
SwapToCurrent(*prev);
|
||||
}
|
||||
|
||||
void Yield() {
|
||||
if (!enabled_) {
|
||||
dbgln("WARN Scheduler skipped yield.");
|
||||
return;
|
||||
}
|
||||
asm volatile("cli");
|
||||
|
||||
SharedPtr<Thread> prev = current_thread_;
|
||||
if (prev == sleep_thread_) {
|
||||
if (runnable_threads_.size() == 0) {
|
||||
panic("Sleep thread yielded without next.");
|
||||
return;
|
||||
} else {
|
||||
// FIXME: Memory operation.
|
||||
current_thread_ = runnable_threads_.PopFront();
|
||||
prev->SetState(Thread::RUNNABLE);
|
||||
}
|
||||
} else {
|
||||
if (runnable_threads_.size() == 0) {
|
||||
current_thread_ = sleep_thread_;
|
||||
dbgln("Sleeping");
|
||||
DumpProcessStates(proc_list_);
|
||||
} else {
|
||||
// FIXME: Memory operation.
|
||||
current_thread_ = runnable_threads_.PopFront();
|
||||
}
|
||||
}
|
||||
|
||||
SwapToCurrent(*prev);
|
||||
}
|
||||
|
||||
private:
|
||||
bool enabled_ = false;
|
||||
// TODO: move this to a separate process manager class.
|
||||
LinkedList<SharedPtr<Process>> proc_list_;
|
||||
|
||||
SharedPtr<Thread> current_thread_;
|
||||
LinkedList<SharedPtr<Thread>> runnable_threads_;
|
||||
|
||||
SharedPtr<Thread> sleep_thread_;
|
||||
};
|
||||
|
||||
static Scheduler* gScheduler = nullptr;
|
||||
|
||||
Scheduler& GetScheduler() {
|
||||
if (!gScheduler) {
|
||||
panic("Scheduler not initialized");
|
||||
}
|
||||
return *gScheduler;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void InitScheduler() { gScheduler = new Scheduler(); }
|
||||
void EnableScheduler() { GetScheduler().Enable(); }
|
||||
Scheduler* gScheduler = nullptr;
|
||||
|
||||
void Preempt() { GetScheduler().Preempt(); }
|
||||
void Yield() { GetScheduler().Yield(); }
|
||||
void Scheduler::Init() { gScheduler = new Scheduler(); }
|
||||
Scheduler::Scheduler() {
|
||||
Process& root = gProcMan->FromId(0);
|
||||
sleep_thread_ = root.GetThread(0);
|
||||
// TODO: Implement a separate sleep thread?
|
||||
current_thread_ = sleep_thread_;
|
||||
}
|
||||
|
||||
void InsertProcess(Process* process) { GetScheduler().InsertProcess(process); }
|
||||
void EnqueueThread(Thread* thread) { GetScheduler().Enqueue(thread); }
|
||||
void Scheduler::SwapToCurrent(Thread& prev) {
|
||||
if (current_thread_->GetState() != Thread::RUNNABLE) {
|
||||
panic("Swapping to non-runnable thread.");
|
||||
}
|
||||
current_thread_->SetState(Thread::RUNNING);
|
||||
|
||||
Process& CurrentProcess() { return GetScheduler().CurrentProcess(); }
|
||||
Thread& CurrentThread() { return GetScheduler().CurrentThread(); }
|
||||
context_switch(prev.Rsp0Ptr(), current_thread_->Rsp0Ptr());
|
||||
|
||||
} // namespace sched
|
||||
asm volatile("sti");
|
||||
}
|
||||
|
||||
void Scheduler::Preempt() {
|
||||
if (!enabled_) {
|
||||
return;
|
||||
}
|
||||
|
||||
asm volatile("cli");
|
||||
if (current_thread_ == sleep_thread_) {
|
||||
// Sleep should never be preempted. (We should yield it if another thread
|
||||
// becomes scheduleable).
|
||||
return;
|
||||
}
|
||||
|
||||
if (runnable_threads_.size() == 0) {
|
||||
// Continue.
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPtr<Thread> prev = current_thread_;
|
||||
prev->SetState(Thread::RUNNABLE);
|
||||
current_thread_ = runnable_threads_.CycleFront(prev);
|
||||
|
||||
SwapToCurrent(*prev);
|
||||
}
|
||||
|
||||
void Scheduler::Yield() {
|
||||
if (!enabled_) {
|
||||
dbgln("WARN Scheduler skipped yield.");
|
||||
return;
|
||||
}
|
||||
asm volatile("cli");
|
||||
|
||||
SharedPtr<Thread> prev = current_thread_;
|
||||
if (prev == sleep_thread_) {
|
||||
if (runnable_threads_.size() == 0) {
|
||||
panic("Sleep thread yielded without next.");
|
||||
return;
|
||||
} else {
|
||||
// FIXME: Memory operation.
|
||||
current_thread_ = runnable_threads_.PopFront();
|
||||
prev->SetState(Thread::RUNNABLE);
|
||||
}
|
||||
} else {
|
||||
if (runnable_threads_.size() == 0) {
|
||||
current_thread_ = sleep_thread_;
|
||||
dbgln("Sleeping");
|
||||
gProcMan->DumpProcessStates();
|
||||
} else {
|
||||
// FIXME: Memory operation.
|
||||
current_thread_ = runnable_threads_.PopFront();
|
||||
}
|
||||
}
|
||||
|
||||
SwapToCurrent(*prev);
|
||||
}
|
||||
|
|
|
@ -3,31 +3,35 @@
|
|||
#include "scheduler/process.h"
|
||||
#include "scheduler/thread.h"
|
||||
|
||||
namespace sched {
|
||||
class Scheduler {
|
||||
public:
|
||||
// Initializes the scheduler in a disabled state.
|
||||
//
|
||||
// Requires the process manager to have been initialized.
|
||||
static void Init();
|
||||
|
||||
// Create the scheduler object in a disabled state,
|
||||
// processes can be added but will not be scheduled.
|
||||
void InitScheduler();
|
||||
void Enable() { enabled_ = true; }
|
||||
|
||||
// Enables the scheduler such that processes will yield on ticks.
|
||||
void EnableScheduler();
|
||||
Process& CurrentProcess() { return current_thread_->process(); }
|
||||
Thread& CurrentThread() { return *current_thread_; }
|
||||
|
||||
// Preempts the current thread and flags it as runnable in the queue.
|
||||
// Generally used by the timer to move to the next timeslice.
|
||||
void Preempt();
|
||||
void Enqueue(const SharedPtr<Thread> thread) {
|
||||
runnable_threads_.PushBack(thread);
|
||||
}
|
||||
|
||||
// Current thread yields and is not rescheduled until some external process
|
||||
// adds it.
|
||||
// Used when a thread blocks or exits.
|
||||
void Yield();
|
||||
void Preempt();
|
||||
void Yield();
|
||||
|
||||
// Scheduler will take ownership
|
||||
// of the created process.
|
||||
void InsertProcess(Process* proc);
|
||||
private:
|
||||
bool enabled_ = false;
|
||||
|
||||
void EnqueueThread(Thread* thread);
|
||||
SharedPtr<Thread> current_thread_;
|
||||
LinkedList<SharedPtr<Thread>> runnable_threads_;
|
||||
|
||||
Process& CurrentProcess();
|
||||
Thread& CurrentThread();
|
||||
SharedPtr<Thread> sleep_thread_;
|
||||
|
||||
} // namespace sched
|
||||
Scheduler();
|
||||
void SwapToCurrent(Thread& prev);
|
||||
};
|
||||
|
||||
extern Scheduler* gScheduler;
|
||||
|
|
|
@ -13,7 +13,7 @@ extern "C" void jump_user_space(uint64_t rip, uint64_t rsp);
|
|||
|
||||
extern "C" void thread_init() {
|
||||
asm("sti");
|
||||
sched::CurrentThread().Init();
|
||||
gScheduler->CurrentThread().Init();
|
||||
panic("Reached end of thread.");
|
||||
}
|
||||
|
||||
|
@ -54,5 +54,5 @@ void Thread::Exit() {
|
|||
dbgln("[%u.%u] Exiting", pid(), id_);
|
||||
state_ = FINISHED;
|
||||
process_->CheckState();
|
||||
sched::Yield();
|
||||
gScheduler->Yield();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ extern "C" void syscall_enter();
|
|||
|
||||
// Used by syscall_enter.s
|
||||
extern "C" uint64_t GetKernelRsp() {
|
||||
return sched::CurrentThread().Rsp0Start();
|
||||
return gScheduler->CurrentThread().Rsp0Start();
|
||||
}
|
||||
|
||||
void InitSyscall() {
|
||||
|
@ -54,7 +54,7 @@ void InitSyscall() {
|
|||
}
|
||||
|
||||
extern "C" void SyscallHandler(uint64_t call_id, char* message) {
|
||||
Thread& thread = sched::CurrentThread();
|
||||
Thread& thread = gScheduler->CurrentThread();
|
||||
switch (call_id) {
|
||||
case Z_THREAD_EXIT:
|
||||
thread.Exit();
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "memory/kernel_heap.h"
|
||||
#include "memory/paging_util.h"
|
||||
#include "memory/physical_memory.h"
|
||||
#include "scheduler/process_manager.h"
|
||||
#include "scheduler/scheduler.h"
|
||||
#include "syscall/syscall.h"
|
||||
|
||||
|
@ -28,16 +29,17 @@ extern "C" void zion() {
|
|||
InitSyscall();
|
||||
|
||||
dbgln("[boot] Init scheduler.");
|
||||
ProcessManager::Init();
|
||||
Scheduler::Init();
|
||||
// Schedule every 50ms.
|
||||
SetFrequency(/* hertz= */ 20);
|
||||
sched::InitScheduler();
|
||||
|
||||
dbgln("[boot] Loading sys init program.");
|
||||
LoadInitProgram();
|
||||
|
||||
dbgln("[boot] Init finished, yielding.");
|
||||
sched::EnableScheduler();
|
||||
sched::Yield();
|
||||
gScheduler->Enable();
|
||||
gScheduler->Yield();
|
||||
|
||||
dbgln("Sleeping!");
|
||||
while (1)
|
||||
|
|
Loading…
Reference in New Issue