Cycle through multiple tasks in multiple processes
This commit is contained in:
parent
7a3b4d2d42
commit
d3024211a7
|
@ -98,20 +98,37 @@ void MapPage(uint64_t virt, uint64_t phys) {
|
||||||
*PageTableEntry(virt) = PageAlign(phys) | PRESENT_BIT | READ_WRITE_BIT;
|
*PageTableEntry(virt) = PageAlign(phys) | PRESENT_BIT | READ_WRITE_BIT;
|
||||||
ZeroOutPage(reinterpret_cast<uint64_t*>(virt));
|
ZeroOutPage(reinterpret_cast<uint64_t*>(virt));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t Pml4Index(uint64_t addr) { return (addr >> PML_OFFSET) & 0x1FF; }
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void InitPaging() {
|
void InitPaging() {
|
||||||
uint64_t pml4_addr = 0;
|
uint64_t pml4_addr = 0;
|
||||||
asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr));
|
asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr));
|
||||||
InitializePml4(pml4_addr);
|
uint64_t* pml4_virtual =
|
||||||
|
reinterpret_cast<uint64_t*>(boot::GetHigherHalfDirectMap() + pml4_addr);
|
||||||
|
|
||||||
|
uint64_t recursive_entry = pml4_addr | PRESENT_BIT | READ_WRITE_BIT;
|
||||||
|
pml4_virtual[0x1FE] = recursive_entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializePml4(uint64_t pml4_physical_addr) {
|
void InitializePml4(uint64_t pml4_physical_addr) {
|
||||||
uint64_t* pml4_virtual = reinterpret_cast<uint64_t*>(
|
uint64_t* pml4_virtual = reinterpret_cast<uint64_t*>(
|
||||||
boot::GetHigherHalfDirectMap() + pml4_physical_addr);
|
boot::GetHigherHalfDirectMap() + pml4_physical_addr);
|
||||||
|
|
||||||
|
// Map the recursive entry.
|
||||||
uint64_t recursive_entry = pml4_physical_addr | PRESENT_BIT | READ_WRITE_BIT;
|
uint64_t recursive_entry = pml4_physical_addr | PRESENT_BIT | READ_WRITE_BIT;
|
||||||
pml4_virtual[0x1FE] = recursive_entry;
|
pml4_virtual[0x1FE] = recursive_entry;
|
||||||
|
|
||||||
|
// Map the kernel entry.
|
||||||
|
// This should contain the heap at 0xFFFFFFFF'40000000
|
||||||
|
uint64_t kernel_addr = 0xFFFFFFFF'80000000;
|
||||||
|
pml4_virtual[Pml4Index(kernel_addr)] = *Pml4Entry(kernel_addr);
|
||||||
|
|
||||||
|
// Map the HHDM.
|
||||||
|
// This is necessary to access values off of the kernel stack.
|
||||||
|
uint64_t hhdm = boot::GetHigherHalfDirectMap();
|
||||||
|
pml4_virtual[Pml4Index(hhdm)] = *Pml4Entry(hhdm);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AllocatePage(uint64_t addr) {
|
void AllocatePage(uint64_t addr) {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#include "scheduler/process.h"
|
#include "scheduler/process.h"
|
||||||
|
|
||||||
#include "debug/debug.h"
|
#include "debug/debug.h"
|
||||||
|
#include "memory/paging_util.h"
|
||||||
|
#include "memory/physical_memory.h"
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/scheduler.h"
|
||||||
#include "scheduler/thread.h"
|
#include "scheduler/thread.h"
|
||||||
|
|
||||||
|
@ -23,17 +25,28 @@ Process* Process::RootProcess() {
|
||||||
return proc;
|
return proc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Process::Process() : id_(gNextId++) {
|
||||||
|
cr3_ = phys_mem::AllocatePage();
|
||||||
|
InitializePml4(cr3_);
|
||||||
|
CreateThread();
|
||||||
|
}
|
||||||
|
|
||||||
void Process::CreateThread() {
|
void Process::CreateThread() {
|
||||||
Thread* thread = new Thread(this, next_thread_id_++);
|
Thread* thread = new Thread(this, next_thread_id_++);
|
||||||
|
ThreadEntry* tentry = new ThreadEntry{
|
||||||
ThreadEntry* entry = thread_list_front_;
|
|
||||||
while (entry->next != nullptr) {
|
|
||||||
entry = entry->next;
|
|
||||||
}
|
|
||||||
entry->next = new ThreadEntry{
|
|
||||||
.thread = thread,
|
.thread = thread,
|
||||||
.next = nullptr,
|
.next = nullptr,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
sched::EnqueueThread(thread);
|
sched::EnqueueThread(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,9 @@ class Scheduler {
|
||||||
Thread* prev = current_thread_;
|
Thread* prev = current_thread_;
|
||||||
current_thread_ = current_thread_->next_thread_;
|
current_thread_ = current_thread_->next_thread_;
|
||||||
prev->next_thread_ = nullptr;
|
prev->next_thread_ = nullptr;
|
||||||
Enqueue(prev);
|
if (prev->pid() != 0) {
|
||||||
|
Enqueue(prev);
|
||||||
|
}
|
||||||
context_switch(prev->Rsp0Ptr(), current_thread_->Rsp0Ptr());
|
context_switch(prev->Rsp0Ptr(), current_thread_->Rsp0Ptr());
|
||||||
|
|
||||||
asm volatile("sti");
|
asm volatile("sti");
|
||||||
|
|
|
@ -8,9 +8,8 @@ namespace {
|
||||||
|
|
||||||
extern "C" void thread_init() {
|
extern "C" void thread_init() {
|
||||||
asm("sti");
|
asm("sti");
|
||||||
dbgln("New Thread!");
|
sched::CurrentThread().Init();
|
||||||
sched::Yield();
|
panic("Reached end of thread.");
|
||||||
panic("End of thread.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -32,3 +31,10 @@ Thread::Thread(Process* proc, uint64_t tid) : process_(proc), id_(tid) {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t Thread::pid() { return process_->id(); }
|
uint64_t Thread::pid() { return process_->id(); }
|
||||||
|
|
||||||
|
void Thread::Init() {
|
||||||
|
while (true) {
|
||||||
|
dbgln("[%u.%u]", pid(), id_);
|
||||||
|
sched::Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,9 @@ class Thread {
|
||||||
|
|
||||||
uint64_t* Rsp0Ptr() { return &rsp0_; }
|
uint64_t* Rsp0Ptr() { return &rsp0_; }
|
||||||
|
|
||||||
|
// Called the first time the thread starts up.
|
||||||
|
void Init();
|
||||||
|
|
||||||
// FIXME: Probably make this private.
|
// FIXME: Probably make this private.
|
||||||
Thread* next_thread_;
|
Thread* next_thread_;
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,10 @@ extern "C" void zion() {
|
||||||
phys_mem::InitPhysicalMemoryManager();
|
phys_mem::InitPhysicalMemoryManager();
|
||||||
|
|
||||||
sched::InitScheduler();
|
sched::InitScheduler();
|
||||||
sched::CurrentProcess().CreateThread();
|
Process p1;
|
||||||
|
p1.CreateThread();
|
||||||
|
Process p2;
|
||||||
|
p2.CreateThread();
|
||||||
sched::EnableScheduler();
|
sched::EnableScheduler();
|
||||||
sched::Yield();
|
sched::Yield();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue