2023-05-18 12:43:53 -07:00
|
|
|
#include "scheduler/process.h"
|
|
|
|
|
|
|
|
#include "debug/debug.h"
|
2023-06-06 15:01:31 -07:00
|
|
|
#include "include/zcall.h"
|
2023-05-18 13:56:54 -07:00
|
|
|
#include "memory/paging_util.h"
|
|
|
|
#include "memory/physical_memory.h"
|
2023-05-18 13:28:22 -07:00
|
|
|
#include "scheduler/scheduler.h"
|
2023-05-18 12:43:53 -07:00
|
|
|
#include "scheduler/thread.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static uint64_t gNextId = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-06 19:12:46 -07:00
|
|
|
RefPtr<Process> Process::RootProcess() {
|
|
|
|
RefPtr<Process> proc = MakeRefCounted<Process>(0);
|
2023-05-30 21:27:20 -07:00
|
|
|
proc->threads_.PushBack(Thread::RootThread(*proc));
|
2023-05-18 12:43:53 -07:00
|
|
|
proc->next_thread_id_ = 1;
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
2023-06-06 19:12:46 -07:00
|
|
|
RefPtr<Process> Process::Create() { return MakeRefCounted<Process>(); }
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-06-06 16:27:25 -07:00
|
|
|
Process::Process() : id_(gNextId++), state_(RUNNING) {
|
|
|
|
caps_.PushBack(new Capability(this, Capability::PROCESS, Z_INIT_PROC_SELF,
|
|
|
|
ZC_PROC_SPAWN_PROC | ZC_PROC_SPAWN_THREAD));
|
|
|
|
}
|
2023-05-18 13:56:54 -07:00
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
RefPtr<Thread> Process::CreateThread() {
|
|
|
|
RefPtr<Thread> thread = MakeRefCounted<Thread>(*this, next_thread_id_++);
|
2023-06-06 16:24:03 -07:00
|
|
|
threads_.PushBack(thread);
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
RefPtr<Thread> Process::GetThread(uint64_t tid) {
|
2023-05-29 15:08:02 -07:00
|
|
|
auto iter = threads_.begin();
|
|
|
|
while (iter != threads_.end()) {
|
|
|
|
if (iter->tid() == tid) {
|
|
|
|
return *iter;
|
2023-05-18 12:43:53 -07:00
|
|
|
}
|
2023-05-29 15:08:02 -07:00
|
|
|
++iter;
|
2023-05-18 12:43:53 -07:00
|
|
|
}
|
|
|
|
panic("Bad thread access.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-05-29 13:51:00 -07:00
|
|
|
|
|
|
|
void Process::CheckState() {
|
2023-05-29 15:08:02 -07:00
|
|
|
auto iter = threads_.begin();
|
|
|
|
while (iter != threads_.end()) {
|
|
|
|
if (iter->GetState() != Thread::FINISHED) {
|
2023-05-29 13:51:00 -07:00
|
|
|
return;
|
|
|
|
}
|
2023-05-29 15:08:02 -07:00
|
|
|
++iter;
|
2023-05-29 13:51:00 -07:00
|
|
|
}
|
|
|
|
state_ = FINISHED;
|
|
|
|
}
|
2023-05-30 23:55:42 -07:00
|
|
|
|
|
|
|
SharedPtr<Capability> Process::GetCapability(uint64_t cid) {
|
|
|
|
auto iter = caps_.begin();
|
|
|
|
while (iter != caps_.end()) {
|
|
|
|
if (iter->id() == cid) {
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
dbgln("Bad cap access");
|
|
|
|
return {};
|
|
|
|
}
|
2023-06-06 16:24:03 -07:00
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
uint64_t Process::AddCapability(RefPtr<Thread>& thread) {
|
2023-06-06 16:24:03 -07:00
|
|
|
uint64_t cap_id = next_cap_id_++;
|
|
|
|
caps_.PushBack(
|
2023-06-06 19:05:03 -07:00
|
|
|
new Capability(thread.get(), Capability::THREAD, cap_id, ZC_WRITE));
|
2023-06-06 16:24:03 -07:00
|
|
|
return cap_id;
|
|
|
|
}
|