Compare commits

..

No commits in common. "80d2bf1aaa1f4b75c69e44b6ac671c8e6d592a62" and "a06c9dced4a14cbbbcc52c50e74e95d049263c0c" have entirely different histories.

7 changed files with 125 additions and 242 deletions

View File

@ -1,100 +0,0 @@
#pragma once
#include <stdint.h>
#include "debug/debug.h"
template <typename T>
class LinkedList {
public:
LinkedList() {}
LinkedList(const LinkedList&) = delete;
uint64_t size() const { return size_; }
void PushBack(const T& item) {
size_++;
ListItem* new_item = new ListItem{
.item = item,
.next = nullptr,
};
if (front_ == nullptr) {
front_ = new_item;
return;
}
ListItem* litem = front_;
while (litem->next != nullptr) {
litem = litem->next;
}
litem->next = new_item;
}
T PopFront() {
if (size_ == 0 || front_ == nullptr) {
panic("Popping from empty list");
}
size_--;
ListItem* old_front = front_;
front_ = front_->next;
T ret = old_front->item;
delete old_front;
return ret;
}
T CycleFront() {
if (size_ == 0 || front_ == nullptr) {
panic("Cycling empty list");
}
if (size_ == 1) {
return front_->item;
}
T ret = front_->item;
ListItem* old_front = front_;
ListItem* iter = front_;
front_ = front_->next;
while (iter->next != nullptr) {
iter = iter->next;
}
iter->next = old_front;
old_front->next = nullptr;
return ret;
}
T PeekFront() const { return front_->item; }
struct ListItem {
T item;
ListItem* next;
};
class Iterator {
public:
Iterator(ListItem* item) : item_(item) {}
Iterator next() { return {item_->next}; }
Iterator& operator++() {
item_ = item_->next;
return *this;
}
T& operator*() { return item_->item; }
T& operator->() { return item_->item; }
bool operator==(const Iterator& other) { return item_ == other.item_; }
bool operator!=(const Iterator& other) { return item_ != other.item_; }
private:
ListItem* item_;
};
Iterator begin() { return {front_}; }
Iterator end() { return {nullptr}; }
private:
uint64_t size_ = 0;
ListItem* front_ = nullptr;
};

View File

@ -1,63 +0,0 @@
#pragma once
#include <stdint.h>
#include "debug/debug.h"
template <typename T>
class SharedPtr {
public:
SharedPtr() : init_(false), ptr_(0), ref_cnt_(0) {}
// Takes ownership.
SharedPtr(T* ptr) {
ptr_ = ptr;
ref_cnt_ = new uint64_t(1);
}
SharedPtr(const SharedPtr<T>& other)
: ptr_(other.ptr_), ref_cnt_(other.ref_cnt_) {
(*ref_cnt_)++;
}
SharedPtr& operator=(const SharedPtr<T>& other) {
Cleanup();
ptr_ = other.ptr_;
ref_cnt_ = other.ref_cnt_;
(*ref_cnt_)++;
return *this;
}
~SharedPtr() { Cleanup(); }
T& operator*() { return *ptr_; }
const T& operator*() const { return *ptr_; }
T* operator->() { return ptr_; }
const T* operator->() const { return ptr_; }
T* ptr() { return ptr_; }
bool operator==(const SharedPtr<T>& other) { return ptr_ == other.ptr_; }
bool empty() { return !init_; }
private:
bool init_ = true;
T* ptr_;
uint64_t* ref_cnt_;
void Cleanup() {
if (!init_) {
return;
}
if (--(*ref_cnt_) == 0) {
dbgln("Deleting shared ptr: %m", ptr_);
delete ptr_;
delete ref_cnt_;
}
}
};
template <typename T, class... A>
SharedPtr<T> MakeShared(A... args) {
return {new T(args...)};
}

View File

@ -12,11 +12,14 @@ static uint64_t gNextId = 1;
} }
SharedPtr<Process> Process::RootProcess() { Process* Process::RootProcess() {
uint64_t pml4_addr = 0; uint64_t pml4_addr = 0;
asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr)); asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr));
SharedPtr<Process> proc(new Process(0, pml4_addr)); Process* proc = new Process(0, pml4_addr);
proc->threads_.PushBack(Thread::RootThread(proc.ptr())); proc->thread_list_front_ = new ThreadEntry{
.thread = Thread::RootThread(proc),
.next = nullptr,
};
proc->next_thread_id_ = 1; proc->next_thread_id_ = 1;
return proc; return proc;
@ -30,29 +33,42 @@ Process::Process(uint64_t elf_ptr) : id_(gNextId++), state_(RUNNING) {
void Process::CreateThread(uint64_t elf_ptr) { void Process::CreateThread(uint64_t elf_ptr) {
Thread* thread = new Thread(this, next_thread_id_++, elf_ptr); Thread* thread = new Thread(this, next_thread_id_++, elf_ptr);
threads_.PushBack(thread); ThreadEntry* tentry = new ThreadEntry{
.thread = thread,
.next = nullptr,
};
if (thread_list_front_ == nullptr) {
thread_list_front_ = tentry;
} else {
ThreadEntry* entry = thread_list_front_;
while (entry->next != nullptr) {
entry = entry->next;
}
entry->next = tentry;
}
sched::EnqueueThread(thread); sched::EnqueueThread(thread);
} }
SharedPtr<Thread> Process::GetThread(uint64_t tid) { Thread* Process::GetThread(uint64_t tid) {
auto iter = threads_.begin(); ThreadEntry* entry = thread_list_front_;
while (iter != threads_.end()) { while (entry != nullptr) {
if (iter->tid() == tid) { if (entry->thread->tid() == tid) {
return *iter; return entry->thread;
} }
++iter;
} }
panic("Bad thread access."); panic("Bad thread access.");
return nullptr; return nullptr;
} }
void Process::CheckState() { void Process::CheckState() {
auto iter = threads_.begin(); ThreadEntry* entry = thread_list_front_;
while (iter != threads_.end()) {
if (iter->GetState() != Thread::FINISHED) { while (entry != nullptr) {
if (entry->thread->GetState() != Thread::FINISHED) {
return; return;
} }
++iter; entry = entry->next;
} }
state_ = FINISHED; state_ = FINISHED;
} }

View File

@ -2,9 +2,6 @@
#include <stdint.h> #include <stdint.h>
#include "lib/linked_list.h"
#include "lib/shared_ptr.h"
// Forward decl due to cyclic dependency. // Forward decl due to cyclic dependency.
class Thread; class Thread;
@ -16,14 +13,15 @@ class Process {
RUNNING, RUNNING,
FINISHED, FINISHED,
}; };
static SharedPtr<Process> RootProcess(); // Caller takes ownership of returned process.
static Process* RootProcess();
Process(uint64_t elf_ptr); Process(uint64_t elf_ptr);
uint64_t id() const { return id_; } uint64_t id() { return id_; }
uint64_t cr3() const { return cr3_; } uint64_t cr3() { return cr3_; }
void CreateThread(uint64_t elf_ptr); void CreateThread(uint64_t elf_ptr);
SharedPtr<Thread> GetThread(uint64_t tid); Thread* GetThread(uint64_t tid);
// Checks the state of all child threads and transitions to // Checks the state of all child threads and transitions to
// finished if all have finished. // finished if all have finished.
@ -39,5 +37,10 @@ class Process {
uint64_t next_thread_id_ = 0; uint64_t next_thread_id_ = 0;
LinkedList<SharedPtr<Thread>> threads_; // FIXME: Make a better data structure for this.
struct ThreadEntry {
Thread* thread;
ThreadEntry* next;
};
ThreadEntry* thread_list_front_;
}; };

View File

@ -1,37 +1,77 @@
#include "scheduler/scheduler.h" #include "scheduler/scheduler.h"
#include "debug/debug.h" #include "debug/debug.h"
#include "lib/linked_list.h"
namespace sched { namespace sched {
namespace { namespace {
extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp); extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp);
void DumpProcessStates(LinkedList<SharedPtr<Process>>& proc_list) { // Simple linked list class with the intent of eventually replacing this with a
dbgln("Process States: %u", proc_list.size()); // map.
auto iter = proc_list.begin(); class ProcList {
while (iter != proc_list.end()) { public:
dbgln("%u: %u", iter->id(), iter->GetState()); ProcList() {}
++iter;
// Takes ownership.
void InsertProcess(Process* proc) {
if (front_ == nullptr) {
front_ = new ProcEntry{
.proc = proc,
.next = nullptr,
};
return;
} }
}
ProcEntry* back = front_;
while (back->next != nullptr) {
back = back->next;
}
back->next = new ProcEntry{
.proc = proc,
.next = nullptr,
};
}
void DumpProcessStates() {
ProcEntry* p = front_;
dbgln("Process States:");
while (p != nullptr) {
dbgln("%u: %u", p->proc->id(), p->proc->GetState());
p = p->next;
}
}
private:
struct ProcEntry {
Process* proc;
ProcEntry* next;
};
ProcEntry* front_ = nullptr;
};
class Scheduler { class Scheduler {
public: public:
Scheduler() { Scheduler() {
SharedPtr<Process> root = Process::RootProcess(); Process* root = Process::RootProcess();
sleep_thread_ = root->GetThread(0); current_thread_ = root->GetThread(0);
runnable_threads_.PushBack(sleep_thread_); proc_list_.InsertProcess(Process::RootProcess());
proc_list_.PushBack(Process::RootProcess());
} }
void Enable() { enabled_ = true; } void Enable() { enabled_ = true; }
Process& CurrentProcess() { return CurrentThread().process(); } Process& CurrentProcess() { return current_thread_->process(); }
Thread& CurrentThread() { return *runnable_threads_.PeekFront(); } Thread& CurrentThread() { return *current_thread_; }
void InsertProcess(Process* process) { proc_list_.PushBack(process); } void InsertProcess(Process* process) { proc_list_.InsertProcess(process); }
void Enqueue(Thread* thread) { runnable_threads_.PushBack(thread); } void Enqueue(Thread* thread) {
Thread* back = current_thread_;
while (back->next_thread_ != nullptr) {
back = back->next_thread_;
}
back->next_thread_ = thread;
}
void Yield() { void Yield() {
if (!enabled_) { if (!enabled_) {
@ -39,48 +79,37 @@ class Scheduler {
} }
asm volatile("cli"); asm volatile("cli");
SharedPtr<Thread> prev; if (current_thread_->next_thread_ == nullptr) {
if (CurrentThread().GetState() == Thread::RUNNING) { if (current_thread_->GetState() == Thread::RUNNING) {
prev = runnable_threads_.CycleFront();
prev->SetState(Thread::RUNNABLE);
} else {
// This technically is a memory operation but should only occur when a
// thread is blocking so may be ok?
prev = runnable_threads_.PopFront();
}
SharedPtr<Thread> next;
if (runnable_threads_.size() == 0) {
next = sleep_thread_;
DumpProcessStates(proc_list_);
} else {
next = runnable_threads_.PeekFront();
}
if (next->GetState() != Thread::RUNNABLE) {
panic("Non-runnable thread in the queue");
}
// Needs to be before the next == prev check
// otherwise the active thread will be RUNNABLE instead of RUNNING.
next->SetState(Thread::RUNNING);
if (next == prev) {
dbgln("No next thread, continue"); dbgln("No next thread, continue");
return; return;
} else {
proc_list_.DumpProcessStates();
panic("FIXME: Implement Sleep");
}
} }
context_switch(prev->Rsp0Ptr(), next->Rsp0Ptr()); Thread* prev = current_thread_;
current_thread_ = current_thread_->next_thread_;
prev->next_thread_ = nullptr;
if (prev->pid() != 0 && prev->GetState() == Thread::RUNNING) {
prev->SetState(Thread::RUNNABLE);
Enqueue(prev);
}
if (current_thread_->GetState() != Thread::RUNNABLE) {
panic("Non-runnable thread in the queue");
}
current_thread_->SetState(Thread::RUNNING);
context_switch(prev->Rsp0Ptr(), current_thread_->Rsp0Ptr());
asm volatile("sti"); asm volatile("sti");
} }
private: private:
bool enabled_ = false; bool enabled_ = false;
// TODO: move this to a separate process manager class. ProcList proc_list_;
LinkedList<SharedPtr<Process>> proc_list_;
LinkedList<SharedPtr<Thread>> runnable_threads_;
SharedPtr<Thread> sleep_thread_; Thread* current_thread_;
}; };
static Scheduler* gScheduler = nullptr; static Scheduler* gScheduler = nullptr;

View File

@ -19,11 +19,9 @@ extern "C" void thread_init() {
} // namespace } // namespace
SharedPtr<Thread> Thread::RootThread(Process* root_proc) { Thread* Thread::RootThread(Process* root_proc) { return new Thread(root_proc); }
return new Thread(root_proc);
}
Thread::Thread(const SharedPtr<Process>& proc, uint64_t tid, uint64_t elf_ptr) Thread::Thread(Process* proc, uint64_t tid, uint64_t elf_ptr)
: process_(proc), id_(tid), elf_ptr_(elf_ptr) { : process_(proc), id_(tid), elf_ptr_(elf_ptr) {
uint64_t* stack = new uint64_t[512]; uint64_t* stack = new uint64_t[512];
uint64_t* stack_ptr = stack + 511; uint64_t* stack_ptr = stack + 511;

View File

@ -2,8 +2,6 @@
#include <stdint.h> #include <stdint.h>
#include "lib/shared_ptr.h"
// Forward decl due to cyclic dependency. // Forward decl due to cyclic dependency.
class Process; class Process;
@ -17,10 +15,9 @@ class Thread {
BLOCKED, BLOCKED,
FINISHED, FINISHED,
}; };
static SharedPtr<Thread> RootThread(Process* root_proc); static Thread* RootThread(Process* root_proc);
explicit Thread(const SharedPtr<Process>& proc, uint64_t tid, explicit Thread(Process* proc, uint64_t tid, uint64_t elf_ptr);
uint64_t elf_ptr);
uint64_t tid() { return id_; }; uint64_t tid() { return id_; };
uint64_t pid(); uint64_t pid();
@ -38,10 +35,13 @@ class Thread {
void SetState(State state) { state_ = state; } void SetState(State state) { state_ = state; }
void Exit(); void Exit();
// FIXME: Probably make this private.
Thread* next_thread_;
private: private:
// Special constructor for the root thread only. // Special constructor for the root thread only.
Thread(Process* proc) : process_(proc), id_(0) {} Thread(Process* proc) : process_(proc), id_(0) {}
SharedPtr<Process> process_; Process* process_;
uint64_t id_; uint64_t id_;
State state_ = RUNNABLE; State state_ = RUNNABLE;