acadia/zion/scheduler/process.cpp
Drew Galbraith b9b45c5e45 Add the ability to copy memory to non resident process.
Use/Test this by loading the user space elf from the kernel process
before it starts rather than as a part of the first thread.

This simplifies thread start a fair bit.
2023-05-30 01:27:47 -07:00

57 lines
1.2 KiB
C++

#include "scheduler/process.h"
#include "debug/debug.h"
#include "memory/paging_util.h"
#include "memory/physical_memory.h"
#include "scheduler/scheduler.h"
#include "scheduler/thread.h"
namespace {
static uint64_t gNextId = 1;
}
SharedPtr<Process> Process::RootProcess() {
uint64_t pml4_addr = 0;
asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr));
SharedPtr<Process> proc(new Process(0, pml4_addr));
proc->threads_.PushBack(Thread::RootThread(proc.ptr()));
proc->next_thread_id_ = 1;
return proc;
}
Process::Process() : id_(gNextId++), state_(RUNNING) {
cr3_ = phys_mem::AllocatePage();
InitializePml4(cr3_);
}
void Process::CreateThread(uint64_t entry) {
Thread* thread = new Thread(this, next_thread_id_++, entry);
threads_.PushBack(thread);
gScheduler->Enqueue(thread);
}
SharedPtr<Thread> Process::GetThread(uint64_t tid) {
auto iter = threads_.begin();
while (iter != threads_.end()) {
if (iter->tid() == tid) {
return *iter;
}
++iter;
}
panic("Bad thread access.");
return nullptr;
}
void Process::CheckState() {
auto iter = threads_.begin();
while (iter != threads_.end()) {
if (iter->GetState() != Thread::FINISHED) {
return;
}
++iter;
}
state_ = FINISHED;
}