Add a kernel ELF module and load it in a new process.

Don't yet jump to userspace.
This commit is contained in:
Drew Galbraith 2023-05-29 00:32:54 -07:00
parent f86bbe6ea9
commit aefb4f082b
22 changed files with 223 additions and 16 deletions

View file

@ -25,14 +25,14 @@ Process* Process::RootProcess() {
return proc;
}
Process::Process() : id_(gNextId++) {
Process::Process(uint64_t elf_ptr) : id_(gNextId++) {
cr3_ = phys_mem::AllocatePage();
InitializePml4(cr3_);
CreateThread();
CreateThread(elf_ptr);
}
void Process::CreateThread() {
Thread* thread = new Thread(this, next_thread_id_++);
void Process::CreateThread(uint64_t elf_ptr) {
Thread* thread = new Thread(this, next_thread_id_++, elf_ptr);
ThreadEntry* tentry = new ThreadEntry{
.thread = thread,
.next = nullptr,

View file

@ -9,12 +9,12 @@ class Process {
public:
// Caller takes ownership of returned process.
static Process* RootProcess();
Process();
Process(uint64_t elf_ptr);
uint64_t id() { return id_; }
uint64_t cr3() { return cr3_; }
void CreateThread();
void CreateThread(uint64_t elf_ptr);
Thread* GetThread(uint64_t tid);
private:

View file

@ -55,6 +55,7 @@ class Scheduler {
Process& CurrentProcess() { return current_thread_->process(); }
Thread& CurrentThread() { return *current_thread_; }
void InsertProcess(Process* process) { proc_list_.InsertProcess(process); }
void Enqueue(Thread* thread) {
Thread* back = current_thread_;
while (back->next_thread_ != nullptr) {
@ -108,6 +109,7 @@ void EnableScheduler() { GetScheduler().Enable(); }
void Yield() { GetScheduler().Yield(); }
void InsertProcess(Process* process) { GetScheduler().InsertProcess(process); }
void EnqueueThread(Thread* thread) { GetScheduler().Enqueue(thread); }
Process& CurrentProcess() { return GetScheduler().CurrentProcess(); }

View file

@ -1,6 +1,7 @@
#include "scheduler/thread.h"
#include "debug/debug.h"
#include "loader/elf_loader.h"
#include "scheduler/process.h"
#include "scheduler/scheduler.h"
@ -16,7 +17,8 @@ extern "C" void thread_init() {
Thread* Thread::RootThread(Process* root_proc) { return new Thread(root_proc); }
Thread::Thread(Process* proc, uint64_t tid) : process_(proc), id_(tid) {
Thread::Thread(Process* proc, uint64_t tid, uint64_t elf_ptr)
: process_(proc), id_(tid), elf_ptr_(elf_ptr) {
uint64_t* stack = new uint64_t[512];
uint64_t* stack_ptr = stack + 511;
// 0: rip
@ -34,6 +36,7 @@ Thread::Thread(Process* proc, uint64_t tid) : process_(proc), id_(tid) {
uint64_t Thread::pid() { return process_->id(); }
void Thread::Init() {
LoadElfProgram(elf_ptr_, 0);
while (true) {
dbgln("[%u.%u]", pid(), id_);
sched::Yield();

View file

@ -9,7 +9,7 @@ class Thread {
public:
static Thread* RootThread(Process* root_proc);
explicit Thread(Process* proc, uint64_t tid);
explicit Thread(Process* proc, uint64_t tid, uint64_t elf_ptr);
uint64_t tid() { return id_; };
uint64_t pid();
@ -31,6 +31,8 @@ class Thread {
Process* process_;
uint64_t id_;
uint64_t elf_ptr_;
// Stack pointer to take on resume.
// Stack will contain the full thread context.
uint64_t rsp0_;