Move Process & Thread to the object folder.

This commit is contained in:
Drew Galbraith 2023-06-06 20:18:53 -07:00
parent 4e278a4664
commit b5ad454ad1
12 changed files with 16 additions and 16 deletions

View file

@ -1,79 +0,0 @@
#include "scheduler/process.h"
#include "debug/debug.h"
#include "include/zcall.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;
}
RefPtr<Process> Process::RootProcess() {
RefPtr<Process> proc = MakeRefCounted<Process>(0);
proc->threads_.PushBack(Thread::RootThread(*proc));
proc->next_thread_id_ = 1;
return proc;
}
RefPtr<Process> Process::Create() {
auto proc = MakeRefCounted<Process>();
proc->caps_.PushBack(
new Capability(proc, Capability::PROCESS, Z_INIT_PROC_SELF,
ZC_PROC_SPAWN_PROC | ZC_PROC_SPAWN_THREAD));
return proc;
}
Process::Process() : id_(gNextId++), state_(RUNNING) {}
RefPtr<Thread> Process::CreateThread() {
RefPtr<Thread> thread = MakeRefCounted<Thread>(*this, next_thread_id_++);
threads_.PushBack(thread);
return thread;
}
RefPtr<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;
}
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");
dbgln("Num caps: %u", caps_.size());
return {};
}
uint64_t Process::AddCapability(const RefPtr<Thread>& thread) {
uint64_t cap_id = next_cap_id_++;
caps_.PushBack(new Capability(thread, Capability::THREAD, cap_id, ZC_WRITE));
return cap_id;
}

View file

@ -1,52 +0,0 @@
#pragma once
#include <stdint.h>
#include "capability/capability.h"
#include "lib/linked_list.h"
#include "lib/ref_ptr.h"
#include "lib/shared_ptr.h"
#include "memory/virtual_memory.h"
// Forward decl due to cyclic dependency.
class Thread;
class Process : public KernelObject {
public:
enum State {
UNSPECIFIED,
SETUP,
RUNNING,
FINISHED,
};
static RefPtr<Process> RootProcess();
static RefPtr<Process> Create();
uint64_t id() const { return id_; }
VirtualMemory& vmm() { return vmm_; }
RefPtr<Thread> CreateThread();
RefPtr<Thread> GetThread(uint64_t tid);
SharedPtr<Capability> GetCapability(uint64_t cid);
uint64_t AddCapability(const RefPtr<Thread>& t);
// Checks the state of all child threads and transitions to
// finished if all have finished.
void CheckState();
State GetState() { return state_; }
private:
friend class MakeRefCountedFriend<Process>;
Process();
Process(uint64_t id) : id_(id), vmm_(VirtualMemory::ForRoot()) {}
uint64_t id_;
VirtualMemory vmm_;
State state_;
uint64_t next_thread_id_ = 0;
uint64_t next_cap_id_ = 0x100;
LinkedList<RefPtr<Thread>> threads_;
LinkedList<SharedPtr<Capability>> caps_;
};

View file

@ -2,7 +2,7 @@
#include "lib/linked_list.h"
#include "lib/ref_ptr.h"
#include "scheduler/process.h"
#include "object/process.h"
class ProcessManager {
public:

View file

@ -1,7 +1,7 @@
#pragma once
#include "scheduler/process.h"
#include "scheduler/thread.h"
#include "object/process.h"
#include "object/thread.h"
class Scheduler {
public:

View file

@ -1,68 +0,0 @@
#include "scheduler/thread.h"
#include "common/gdt.h"
#include "debug/debug.h"
#include "loader/elf_loader.h"
#include "memory/paging_util.h"
#include "scheduler/process.h"
#include "scheduler/scheduler.h"
namespace {
extern "C" void jump_user_space(uint64_t rip, uint64_t rsp, uint64_t arg1,
uint64_t arg2);
extern "C" void thread_init() {
asm("sti");
gScheduler->CurrentThread().Init();
panic("Reached end of thread.");
}
} // namespace
RefPtr<Thread> Thread::RootThread(Process& root_proc) {
return MakeRefCounted<Thread>(root_proc);
}
RefPtr<Thread> Thread::Create(Process& proc, uint64_t tid) {
return MakeRefCounted<Thread>(proc, tid);
}
Thread::Thread(Process& proc, uint64_t tid) : process_(proc), id_(tid) {
uint64_t* stack_ptr = proc.vmm().AllocateKernelStack();
// 0: rip
*(stack_ptr) = reinterpret_cast<uint64_t>(thread_init);
// 1-4: rax, rcx, rdx, rbx
// 5: rbp
*(stack_ptr - 5) = reinterpret_cast<uint64_t>(stack_ptr + 1);
// 6-15: rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15
// 16: cr3
*(stack_ptr - 16) = proc.vmm().cr3();
rsp0_ = reinterpret_cast<uint64_t>(stack_ptr - 16);
rsp0_start_ = reinterpret_cast<uint64_t>(stack_ptr);
}
uint64_t Thread::pid() const { return process_.id(); }
void Thread::Start(uint64_t entry, uint64_t arg1, uint64_t arg2) {
rip_ = entry;
arg1_ = arg1;
arg2_ = arg2;
state_ = RUNNABLE;
// Get from parent to avoid creating a new shared ptr.
gScheduler->Enqueue(process_.GetThread(id_));
}
void Thread::Init() {
dbgln("Thread start.", pid(), id_);
uint64_t rsp = process_.vmm().AllocateUserStack();
SetRsp0(rsp0_start_);
jump_user_space(rip_, rsp, arg1_, arg2_);
}
void Thread::Exit() {
dbgln("Exiting", pid(), id_);
state_ = FINISHED;
process_.CheckState();
gScheduler->Yield();
}

View file

@ -1,62 +0,0 @@
#pragma once
#include <stdint.h>
#include "lib/ref_ptr.h"
#include "object/kernel_object.h"
// Forward decl due to cyclic dependency.
class Process;
class Thread : public KernelObject {
public:
enum State {
UNSPECIFIED,
CREATED,
RUNNING,
RUNNABLE,
FINISHED,
};
static RefPtr<Thread> RootThread(Process& root_proc);
static RefPtr<Thread> Create(Process& proc, uint64_t tid);
uint64_t tid() const { return id_; };
uint64_t pid() const;
Process& process() { return process_; }
uint64_t* Rsp0Ptr() { return &rsp0_; }
uint64_t Rsp0Start() { return rsp0_start_; }
// Switches the thread's state to runnable and enqueues it.
void Start(uint64_t entry, uint64_t arg1, uint64_t arg2);
// Called the first time the thread starts up.
void Init();
// State Management.
State GetState() { return state_; };
void SetState(State state) { state_ = state; }
void Exit();
private:
friend class MakeRefCountedFriend<Thread>;
Thread(Process& proc, uint64_t tid);
// Special constructor for the root thread only.
Thread(Process& proc) : process_(proc), id_(0) {}
Process& process_;
uint64_t id_;
State state_ = CREATED;
// Startup Context for the thread.
uint64_t rip_;
uint64_t arg1_;
uint64_t arg2_;
// Stack pointer to take on resume.
// Stack will contain the full thread context.
uint64_t rsp0_;
// Stack pointer to take when returning from userspace.
// I don't think me mind clobbering the stack here.
uint64_t rsp0_start_;
};