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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Process* Process::RootProcess() {
|
|
|
|
uint64_t pml4_addr = 0;
|
|
|
|
asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr));
|
|
|
|
Process* proc = new Process(0, pml4_addr);
|
|
|
|
proc->thread_list_front_ = new ThreadEntry{
|
2023-05-18 13:24:02 -07:00
|
|
|
.thread = Thread::RootThread(proc),
|
2023-05-18 12:43:53 -07:00
|
|
|
.next = nullptr,
|
|
|
|
};
|
|
|
|
proc->next_thread_id_ = 1;
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2023-05-29 00:32:54 -07:00
|
|
|
Process::Process(uint64_t elf_ptr) : id_(gNextId++) {
|
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-18 13:56:54 -07:00
|
|
|
ThreadEntry* tentry = new ThreadEntry{
|
2023-05-18 13:24:02 -07:00
|
|
|
.thread = thread,
|
|
|
|
.next = nullptr,
|
|
|
|
};
|
2023-05-18 13:56:54 -07:00
|
|
|
|
|
|
|
if (thread_list_front_ == nullptr) {
|
|
|
|
thread_list_front_ = tentry;
|
|
|
|
} else {
|
|
|
|
ThreadEntry* entry = thread_list_front_;
|
|
|
|
while (entry->next != nullptr) {
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
entry->next = tentry;
|
|
|
|
}
|
2023-05-18 13:28:22 -07:00
|
|
|
sched::EnqueueThread(thread);
|
2023-05-18 13:24:02 -07:00
|
|
|
}
|
|
|
|
|
2023-05-18 12:43:53 -07:00
|
|
|
Thread* Process::GetThread(uint64_t tid) {
|
|
|
|
ThreadEntry* entry = thread_list_front_;
|
|
|
|
while (entry != nullptr) {
|
|
|
|
if (entry->thread->tid() == tid) {
|
|
|
|
return entry->thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("Bad thread access.");
|
|
|
|
return nullptr;
|
|
|
|
}
|