2023-05-18 12:43:53 -07:00
|
|
|
#include "scheduler/process.h"
|
|
|
|
|
|
|
|
#include "debug/debug.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-05-29 15:50:38 -07:00
|
|
|
SharedPtr<Process> Process::RootProcess() {
|
2023-05-18 12:43:53 -07:00
|
|
|
uint64_t pml4_addr = 0;
|
|
|
|
asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr));
|
2023-05-29 15:50:38 -07:00
|
|
|
SharedPtr<Process> proc(new Process(0, pml4_addr));
|
|
|
|
proc->threads_.PushBack(Thread::RootThread(proc.ptr()));
|
2023-05-18 12:43:53 -07:00
|
|
|
proc->next_thread_id_ = 1;
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2023-05-29 13:51:00 -07:00
|
|
|
Process::Process(uint64_t elf_ptr) : id_(gNextId++), state_(RUNNING) {
|
2023-05-18 13:56:54 -07:00
|
|
|
cr3_ = phys_mem::AllocatePage();
|
|
|
|
InitializePml4(cr3_);
|
2023-05-29 00:32:54 -07:00
|
|
|
CreateThread(elf_ptr);
|
2023-05-18 13:56:54 -07:00
|
|
|
}
|
|
|
|
|
2023-05-29 00:32:54 -07:00
|
|
|
void Process::CreateThread(uint64_t elf_ptr) {
|
|
|
|
Thread* thread = new Thread(this, next_thread_id_++, elf_ptr);
|
2023-05-29 15:08:02 -07:00
|
|
|
threads_.PushBack(thread);
|
2023-05-18 13:28:22 -07:00
|
|
|
sched::EnqueueThread(thread);
|
2023-05-18 13:24:02 -07:00
|
|
|
}
|
|
|
|
|
2023-05-29 15:50:38 -07:00
|
|
|
SharedPtr<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;
|
|
|
|
}
|