2023-06-06 20:18:53 -07:00
|
|
|
#include "object/process.h"
|
2023-05-18 12:43:53 -07:00
|
|
|
|
|
|
|
#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-06-06 20:18:53 -07:00
|
|
|
#include "object/thread.h"
|
2023-05-18 13:28:22 -07:00
|
|
|
#include "scheduler/scheduler.h"
|
2023-05-18 12:43:53 -07:00
|
|
|
|
|
|
|
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 20:13:07 -07:00
|
|
|
RefPtr<Process> Process::Create() {
|
|
|
|
auto proc = MakeRefCounted<Process>();
|
|
|
|
proc->caps_.PushBack(
|
2023-06-07 06:21:36 -07:00
|
|
|
MakeRefCounted<Capability>(proc, Capability::PROCESS, Z_INIT_PROC_SELF,
|
|
|
|
ZC_PROC_SPAWN_PROC | ZC_PROC_SPAWN_THREAD));
|
|
|
|
proc->caps_.PushBack(MakeRefCounted<Capability>(
|
|
|
|
proc->vmas(), Capability::ADDRESS_SPACE, Z_INIT_VMAS_SELF, ZC_WRITE));
|
2023-06-06 20:13:07 -07:00
|
|
|
return proc;
|
2023-06-06 16:27:25 -07:00
|
|
|
}
|
2023-05-18 13:56:54 -07:00
|
|
|
|
2023-06-07 00:04:53 -07:00
|
|
|
Process::Process()
|
2023-06-07 00:30:26 -07:00
|
|
|
: id_(gNextId++), vmas_(MakeRefCounted<AddressSpace>()), state_(RUNNING) {}
|
2023-06-06 20:13:07 -07:00
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
RefPtr<Thread> Process::CreateThread() {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-06 19:05:03 -07:00
|
|
|
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-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
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-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
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
|
|
|
|
2023-06-07 08:24:10 -07:00
|
|
|
RefPtr<Capability> Process::ReleaseCapability(uint64_t cid) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-07 08:24:10 -07:00
|
|
|
auto iter = caps_.begin();
|
|
|
|
while (iter != caps_.end()) {
|
|
|
|
if (*iter && iter->id() == cid) {
|
|
|
|
auto cap = *iter;
|
|
|
|
*iter = {nullptr};
|
|
|
|
return cap;
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
2023-06-15 16:20:29 -07:00
|
|
|
dbgln("Bad cap release: %u", cid);
|
2023-06-07 08:24:10 -07:00
|
|
|
dbgln("Num caps: %u", caps_.size());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-06-07 06:21:36 -07:00
|
|
|
RefPtr<Capability> Process::GetCapability(uint64_t cid) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-05-30 23:55:42 -07:00
|
|
|
auto iter = caps_.begin();
|
|
|
|
while (iter != caps_.end()) {
|
2023-06-07 08:24:10 -07:00
|
|
|
if (*iter && iter->id() == cid) {
|
2023-05-30 23:55:42 -07:00
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
2023-06-15 16:20:29 -07:00
|
|
|
dbgln("Bad cap access %u", cid);
|
2023-06-06 20:13:07 -07:00
|
|
|
dbgln("Num caps: %u", caps_.size());
|
2023-05-30 23:55:42 -07:00
|
|
|
return {};
|
|
|
|
}
|
2023-06-06 16:24:03 -07:00
|
|
|
|
2023-06-07 08:24:10 -07:00
|
|
|
uint64_t Process::AddCapability(const RefPtr<Capability>& cap) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-07 08:24:10 -07:00
|
|
|
cap->set_id(next_cap_id_++);
|
|
|
|
caps_.PushBack(cap);
|
|
|
|
return cap->id();
|
|
|
|
}
|
2023-06-06 20:13:07 -07:00
|
|
|
uint64_t Process::AddCapability(const RefPtr<Thread>& thread) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-06 16:24:03 -07:00
|
|
|
uint64_t cap_id = next_cap_id_++;
|
2023-06-07 06:21:36 -07:00
|
|
|
caps_.PushBack(
|
|
|
|
MakeRefCounted<Capability>(thread, Capability::THREAD, cap_id, ZC_WRITE));
|
2023-06-06 16:24:03 -07:00
|
|
|
return cap_id;
|
|
|
|
}
|
2023-06-07 00:04:53 -07:00
|
|
|
|
|
|
|
uint64_t Process::AddCapability(const RefPtr<Process>& p) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-07 00:04:53 -07:00
|
|
|
uint64_t cap_id = next_cap_id_++;
|
2023-06-07 06:21:36 -07:00
|
|
|
caps_.PushBack(MakeRefCounted<Capability>(p, Capability::PROCESS, cap_id,
|
|
|
|
ZC_WRITE | ZC_PROC_SPAWN_THREAD));
|
2023-06-07 00:04:53 -07:00
|
|
|
return cap_id;
|
|
|
|
}
|
2023-06-07 00:30:26 -07:00
|
|
|
uint64_t Process::AddCapability(const RefPtr<AddressSpace>& vmas) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-07 00:04:53 -07:00
|
|
|
uint64_t cap_id = next_cap_id_++;
|
2023-06-07 06:21:36 -07:00
|
|
|
caps_.PushBack(MakeRefCounted<Capability>(vmas, Capability::ADDRESS_SPACE,
|
|
|
|
cap_id, ZC_WRITE));
|
2023-06-07 00:04:53 -07:00
|
|
|
return cap_id;
|
|
|
|
}
|
2023-06-07 00:30:26 -07:00
|
|
|
uint64_t Process::AddCapability(const RefPtr<MemoryObject>& vmmo) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-07 00:04:53 -07:00
|
|
|
uint64_t cap_id = next_cap_id_++;
|
2023-06-07 06:21:36 -07:00
|
|
|
caps_.PushBack(MakeRefCounted<Capability>(vmmo, Capability::MEMORY_OBJECT,
|
|
|
|
cap_id, ZC_WRITE));
|
2023-06-07 00:04:53 -07:00
|
|
|
return cap_id;
|
|
|
|
}
|
2023-06-07 08:24:10 -07:00
|
|
|
uint64_t Process::AddCapability(const RefPtr<Channel>& chan) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-07 08:24:10 -07:00
|
|
|
uint64_t cap_id = next_cap_id_++;
|
|
|
|
caps_.PushBack(MakeRefCounted<Capability>(chan, Capability::CHANNEL, cap_id,
|
|
|
|
ZC_WRITE | ZC_READ));
|
|
|
|
return cap_id;
|
|
|
|
}
|
2023-06-07 00:19:15 -07:00
|
|
|
|
2023-06-12 19:20:51 -07:00
|
|
|
uint64_t Process::AddCapability(const RefPtr<Port>& port) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-12 19:20:51 -07:00
|
|
|
uint64_t cap_id = next_cap_id_++;
|
|
|
|
caps_.PushBack(MakeRefCounted<Capability>(port, Capability::PORT, cap_id,
|
|
|
|
ZC_WRITE | ZC_READ));
|
|
|
|
return cap_id;
|
|
|
|
}
|
|
|
|
|
2023-06-07 00:30:26 -07:00
|
|
|
void Process::AddCapability(uint64_t cap_id, const RefPtr<MemoryObject>& vmmo) {
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-07 06:21:36 -07:00
|
|
|
caps_.PushBack(MakeRefCounted<Capability>(vmmo, Capability::MEMORY_OBJECT,
|
|
|
|
cap_id, ZC_WRITE));
|
2023-06-07 00:19:15 -07:00
|
|
|
}
|