[Zion] Add a framework for better process exit.
This commit is contained in:
parent
aa2d80b557
commit
308dd6a203
|
@ -16,6 +16,7 @@ class KernelVmm {
|
||||||
|
|
||||||
static uint64_t AcquireKernelStack();
|
static uint64_t AcquireKernelStack();
|
||||||
|
|
||||||
|
// Takes the base address to the stack. I.e. the highest value in it.
|
||||||
static void FreeKernelStack(uint64_t);
|
static void FreeKernelStack(uint64_t);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include "memory/paging_util.h"
|
#include "memory/paging_util.h"
|
||||||
#include "memory/physical_memory.h"
|
#include "memory/physical_memory.h"
|
||||||
#include "object/thread.h"
|
#include "object/thread.h"
|
||||||
|
#include "scheduler/process_manager.h"
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/scheduler.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -49,11 +50,11 @@ glcr::RefPtr<Thread> Process::GetThread(uint64_t tid) {
|
||||||
void Process::CheckState() {
|
void Process::CheckState() {
|
||||||
MutexHolder lock(mutex_);
|
MutexHolder lock(mutex_);
|
||||||
for (uint64_t i = 0; i < threads_.size(); i++) {
|
for (uint64_t i = 0; i < threads_.size(); i++) {
|
||||||
if (threads_[i]->GetState() != Thread::FINISHED) {
|
if (!threads_[i]->IsDying()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
state_ = FINISHED;
|
Exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::RefPtr<Capability> Process::ReleaseCapability(uint64_t cid) {
|
glcr::RefPtr<Capability> Process::ReleaseCapability(uint64_t cid) {
|
||||||
|
@ -67,3 +68,28 @@ glcr::RefPtr<Capability> Process::GetCapability(uint64_t cid) {
|
||||||
uint64_t Process::AddExistingCapability(const glcr::RefPtr<Capability>& cap) {
|
uint64_t Process::AddExistingCapability(const glcr::RefPtr<Capability>& cap) {
|
||||||
return caps_.AddExistingCapability(cap);
|
return caps_.AddExistingCapability(cap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Process::Exit() {
|
||||||
|
// TODO: Check this state elsewhere to ensure that we don't for instance
|
||||||
|
// create a running thread on a finished process.
|
||||||
|
state_ = FINISHED;
|
||||||
|
|
||||||
|
for (uint64_t i = 0; i < threads_.size(); i++) {
|
||||||
|
if (!threads_[i]->IsDying()) {
|
||||||
|
threads_[i]->Cleanup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// From this point onward no threads should be able to reach userspace.
|
||||||
|
|
||||||
|
// TODO: Unmap all userspace mappings.
|
||||||
|
// TODO: Clear capabilities.
|
||||||
|
|
||||||
|
// TODO: In the future consider removing this from the process manager.
|
||||||
|
// I need to think through the implications because the process object
|
||||||
|
// will be kept alive by the process that created it most likely.
|
||||||
|
|
||||||
|
if (gScheduler->CurrentProcess().id_ == id_) {
|
||||||
|
gScheduler->Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -61,6 +61,8 @@ class Process : public KernelObject {
|
||||||
|
|
||||||
State GetState() { return state_; }
|
State GetState() { return state_; }
|
||||||
|
|
||||||
|
void Exit();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class glcr::MakeRefCountedFriend<Process>;
|
friend class glcr::MakeRefCountedFriend<Process>;
|
||||||
Process();
|
Process();
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "common/gdt.h"
|
#include "common/gdt.h"
|
||||||
#include "debug/debug.h"
|
#include "debug/debug.h"
|
||||||
|
#include "memory/kernel_vmm.h"
|
||||||
#include "memory/paging_util.h"
|
#include "memory/paging_util.h"
|
||||||
#include "object/process.h"
|
#include "object/process.h"
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/scheduler.h"
|
||||||
|
@ -68,14 +69,27 @@ void Thread::Exit() {
|
||||||
#if K_THREAD_DEBUG
|
#if K_THREAD_DEBUG
|
||||||
dbgln("Exiting");
|
dbgln("Exiting");
|
||||||
#endif
|
#endif
|
||||||
state_ = FINISHED;
|
auto curr_thread = gScheduler->CurrentThread();
|
||||||
|
if (curr_thread->tid() != id_) {
|
||||||
|
panic("Thread::Exit called from [{}.{}] on [{}.{}]", curr_thread->pid(),
|
||||||
|
curr_thread->tid(), pid(), tid());
|
||||||
|
}
|
||||||
|
Cleanup();
|
||||||
|
gScheduler->Yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Thread::Cleanup() {
|
||||||
|
state_ = CLEANUP;
|
||||||
process_.CheckState();
|
process_.CheckState();
|
||||||
while (blocked_threads_.size() != 0) {
|
while (blocked_threads_.size() != 0) {
|
||||||
auto thread = blocked_threads_.PopFront();
|
auto thread = blocked_threads_.PopFront();
|
||||||
thread->SetState(Thread::RUNNABLE);
|
thread->SetState(Thread::RUNNABLE);
|
||||||
gScheduler->Enqueue(thread);
|
gScheduler->Enqueue(thread);
|
||||||
}
|
}
|
||||||
gScheduler->Yield();
|
state_ = FINISHED;
|
||||||
|
// TODO: Race condition when called from exit, once kernel stack manager
|
||||||
|
// actually reuses stacks this will cause an issue
|
||||||
|
KernelVmm::FreeKernelStack(rsp0_start_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thread::Wait() {
|
void Thread::Wait() {
|
||||||
|
@ -86,7 +100,7 @@ void Thread::Wait() {
|
||||||
// 3. B finishes.
|
// 3. B finishes.
|
||||||
// 4. Context Switch B -> A
|
// 4. Context Switch B -> A
|
||||||
// 5. A forever blocks on B.
|
// 5. A forever blocks on B.
|
||||||
if (state_ == Thread::FINISHED) {
|
if (IsDying()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto thread = gScheduler->CurrentThread();
|
auto thread = gScheduler->CurrentThread();
|
||||||
|
|
|
@ -29,6 +29,7 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
|
||||||
RUNNING,
|
RUNNING,
|
||||||
RUNNABLE,
|
RUNNABLE,
|
||||||
BLOCKED,
|
BLOCKED,
|
||||||
|
CLEANUP,
|
||||||
FINISHED,
|
FINISHED,
|
||||||
};
|
};
|
||||||
static glcr::RefPtr<Thread> RootThread(Process& root_proc);
|
static glcr::RefPtr<Thread> RootThread(Process& root_proc);
|
||||||
|
@ -51,8 +52,17 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
|
||||||
// State Management.
|
// State Management.
|
||||||
State GetState() { return state_; };
|
State GetState() { return state_; };
|
||||||
void SetState(State state) { state_ = state; }
|
void SetState(State state) { state_ = state; }
|
||||||
|
bool IsDying() { return state_ == CLEANUP || state_ == FINISHED; }
|
||||||
|
|
||||||
|
// Exits this thread.
|
||||||
|
// Allows all blocked threads to run and releases the kernel stack.
|
||||||
|
// This function should only be called by the running thread on itself
|
||||||
|
// as it will yield.
|
||||||
void Exit();
|
void Exit();
|
||||||
|
|
||||||
|
// Like Exit except it does not yield.
|
||||||
|
void Cleanup();
|
||||||
|
|
||||||
void Wait();
|
void Wait();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -12,6 +12,8 @@ class ProcessManager {
|
||||||
static void Init();
|
static void Init();
|
||||||
|
|
||||||
void InsertProcess(const glcr::RefPtr<Process>& proc);
|
void InsertProcess(const glcr::RefPtr<Process>& proc);
|
||||||
|
void RemoveProcess(uint64_t id);
|
||||||
|
|
||||||
Process& FromId(uint64_t id);
|
Process& FromId(uint64_t id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -43,6 +43,8 @@ void Scheduler::Preempt() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ClearDeadThreadsFromFront();
|
||||||
|
|
||||||
asm volatile("cli");
|
asm volatile("cli");
|
||||||
if (current_thread_ == sleep_thread_) {
|
if (current_thread_ == sleep_thread_) {
|
||||||
// Sleep should never be preempted. (We should yield it if another thread
|
// Sleep should never be preempted. (We should yield it if another thread
|
||||||
|
@ -66,9 +68,14 @@ void Scheduler::Preempt() {
|
||||||
|
|
||||||
void Scheduler::Yield() {
|
void Scheduler::Yield() {
|
||||||
if (!enabled_) {
|
if (!enabled_) {
|
||||||
|
// This is expected to fire once at the start when we enqueue the first
|
||||||
|
// thread before the scheduler is enabled. Maybe we should get rid of it?
|
||||||
dbgln("WARN Scheduler skipped yield.");
|
dbgln("WARN Scheduler skipped yield.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ClearDeadThreadsFromFront();
|
||||||
|
|
||||||
asm volatile("cli");
|
asm volatile("cli");
|
||||||
|
|
||||||
glcr::RefPtr<Thread> prev = current_thread_;
|
glcr::RefPtr<Thread> prev = current_thread_;
|
||||||
|
@ -78,8 +85,10 @@ void Scheduler::Yield() {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
current_thread_ = runnable_threads_.PopFront();
|
current_thread_ = runnable_threads_.PopFront();
|
||||||
|
if (!prev->IsDying()) {
|
||||||
prev->SetState(Thread::RUNNABLE);
|
prev->SetState(Thread::RUNNABLE);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (runnable_threads_.size() == 0) {
|
if (runnable_threads_.size() == 0) {
|
||||||
current_thread_ = sleep_thread_;
|
current_thread_ = sleep_thread_;
|
||||||
|
@ -90,3 +99,10 @@ void Scheduler::Yield() {
|
||||||
|
|
||||||
SwapToCurrent(*prev);
|
SwapToCurrent(*prev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Scheduler::ClearDeadThreadsFromFront() {
|
||||||
|
while (runnable_threads_.size() > 0 &&
|
||||||
|
runnable_threads_.PeekFront()->IsDying()) {
|
||||||
|
runnable_threads_.PopFront();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -33,6 +33,8 @@ class Scheduler {
|
||||||
|
|
||||||
Scheduler();
|
Scheduler();
|
||||||
void SwapToCurrent(Thread& prev);
|
void SwapToCurrent(Thread& prev);
|
||||||
|
|
||||||
|
void ClearDeadThreadsFromFront();
|
||||||
};
|
};
|
||||||
|
|
||||||
extern Scheduler* gScheduler;
|
extern Scheduler* gScheduler;
|
||||||
|
|
|
@ -8,8 +8,7 @@
|
||||||
z_err_t ProcessExit(ZProcessExitReq* req) {
|
z_err_t ProcessExit(ZProcessExitReq* req) {
|
||||||
auto curr_thread = gScheduler->CurrentThread();
|
auto curr_thread = gScheduler->CurrentThread();
|
||||||
dbgln("Exit code: {x}", req->code);
|
dbgln("Exit code: {x}", req->code);
|
||||||
// FIXME: kill process here.
|
curr_thread->process().Exit();
|
||||||
curr_thread->Exit();
|
|
||||||
panic("Returned from thread exit");
|
panic("Returned from thread exit");
|
||||||
return glcr::UNIMPLEMENTED;
|
return glcr::UNIMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue