acadia/zion/object/process.cpp

96 lines
2.4 KiB
C++
Raw Normal View History

#include "object/process.h"
#include "debug/debug.h"
2023-06-06 15:01:31 -07:00
#include "include/zcall.h"
#include "memory/paging_util.h"
#include "memory/physical_memory.h"
#include "object/thread.h"
#include "scheduler/process_manager.h"
#include "scheduler/scheduler.h"
namespace {
static uint64_t gNextId = 1;
}
2023-06-21 15:07:40 -07:00
glcr::RefPtr<Process> Process::RootProcess() {
glcr::RefPtr<Process> proc = glcr::MakeRefCounted<Process>(0);
proc->threads_.PushBack(Thread::RootThread(*proc));
proc->next_thread_id_ = 1;
return proc;
}
2023-06-21 15:07:40 -07:00
glcr::RefPtr<Process> Process::Create() {
return glcr::MakeRefCounted<Process>();
}
Process::Process()
2023-06-21 15:07:40 -07:00
: id_(gNextId++),
vmas_(glcr::MakeRefCounted<AddressSpace>()),
state_(RUNNING) {}
2023-06-21 15:07:40 -07:00
glcr::RefPtr<Thread> Process::CreateThread() {
MutexHolder lock(mutex_);
2023-06-21 15:07:40 -07:00
glcr::RefPtr<Thread> thread =
glcr::MakeRefCounted<Thread>(*this, next_thread_id_++);
2023-06-06 16:24:03 -07:00
threads_.PushBack(thread);
return thread;
}
2023-06-21 15:07:40 -07:00
glcr::RefPtr<Thread> Process::GetThread(uint64_t tid) {
MutexHolder lock(mutex_);
2023-06-26 15:46:03 -07:00
if (tid >= threads_.size()) {
panic("Bad thread access {} on process {} with {} threads.", tid, id_,
2023-06-26 15:46:03 -07:00
threads_.size());
}
2023-06-26 15:46:03 -07:00
return threads_[tid];
}
void Process::CheckState() {
MutexHolder lock(mutex_);
2023-06-26 15:46:03 -07:00
for (uint64_t i = 0; i < threads_.size(); i++) {
if (!threads_[i]->IsDying()) {
return;
}
}
Exit();
}
2023-06-21 15:07:40 -07:00
glcr::RefPtr<Capability> Process::ReleaseCapability(uint64_t cid) {
return caps_.ReleaseCapability(cid);
}
2023-06-21 15:07:40 -07:00
glcr::RefPtr<Capability> Process::GetCapability(uint64_t cid) {
return caps_.GetCapability(cid);
}
2023-06-06 16:24:03 -07:00
2023-06-21 15:07:40 -07:00
uint64_t Process::AddExistingCapability(const glcr::RefPtr<Capability>& 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();
}
}