Manage KernelStacks separately rather than just allocing bytes.

Create a global KernelStackManager that will handle the relevant allocs.
This commit is contained in:
Drew Galbraith 2023-05-30 21:27:20 -07:00
parent 3c3341a90f
commit f22dd66c8d
9 changed files with 104 additions and 24 deletions

View file

@ -16,7 +16,7 @@ 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->threads_.PushBack(Thread::RootThread(*proc));
proc->next_thread_id_ = 1;
return proc;
@ -28,7 +28,7 @@ Process::Process() : id_(gNextId++), state_(RUNNING) {
}
void Process::CreateThread(uint64_t entry) {
Thread* thread = new Thread(this, next_thread_id_++, entry);
Thread* thread = new Thread(*this, next_thread_id_++, entry);
threads_.PushBack(thread);
gScheduler->Enqueue(thread);
}

View file

@ -19,14 +19,14 @@ extern "C" void thread_init() {
} // namespace
SharedPtr<Thread> Thread::RootThread(Process* root_proc) {
SharedPtr<Thread> Thread::RootThread(Process& root_proc) {
return new Thread(root_proc);
}
Thread::Thread(const SharedPtr<Process>& proc, uint64_t tid, uint64_t entry)
Thread::Thread(Process& proc, uint64_t tid, uint64_t entry)
: process_(proc), id_(tid), rip_(entry) {
uint64_t* stack = new uint64_t[512];
uint64_t* stack_ptr = stack + 511;
uint64_t* stack_ptr = proc.vmm().AllocateKernelStack();
dbgln("Kernel Stack at: %m", stack_ptr);
// 0: rip
*(stack_ptr) = reinterpret_cast<uint64_t>(thread_init);
// 1-4: rax, rcx, rdx, rbx
@ -34,12 +34,12 @@ Thread::Thread(const SharedPtr<Process>& proc, uint64_t tid, uint64_t entry)
*(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->cr3();
*(stack_ptr - 16) = proc.cr3();
rsp0_ = reinterpret_cast<uint64_t>(stack_ptr - 16);
rsp0_start_ = reinterpret_cast<uint64_t>(stack_ptr);
}
uint64_t Thread::pid() { return process_->id(); }
uint64_t Thread::pid() const { return process_.id(); }
void Thread::Init() {
dbgln("[%u.%u] thread start.", pid(), id_);
@ -52,6 +52,6 @@ void Thread::Init() {
void Thread::Exit() {
dbgln("[%u.%u] Exiting", pid(), id_);
state_ = FINISHED;
process_->CheckState();
process_.CheckState();
gScheduler->Yield();
}

View file

@ -15,14 +15,14 @@ class Thread {
RUNNABLE,
FINISHED,
};
static SharedPtr<Thread> RootThread(Process* root_proc);
static SharedPtr<Thread> RootThread(Process& root_proc);
explicit Thread(const SharedPtr<Process>& proc, uint64_t tid, uint64_t entry);
Thread(Process& proc, uint64_t tid, uint64_t entry);
uint64_t tid() { return id_; };
uint64_t pid();
uint64_t tid() const { return id_; };
uint64_t pid() const;
Process& process() { return *process_; }
Process& process() { return process_; }
uint64_t* Rsp0Ptr() { return &rsp0_; }
uint64_t Rsp0Start() { return rsp0_start_; }
@ -37,8 +37,8 @@ class Thread {
private:
// Special constructor for the root thread only.
Thread(Process* proc) : process_(proc), id_(0) {}
SharedPtr<Process> process_;
Thread(Process& proc) : process_(proc), id_(0) {}
Process& process_;
uint64_t id_;
State state_ = RUNNABLE;