From 2dd799b40682a78945d2e6ab7b30c4585830bdd6 Mon Sep 17 00:00:00 2001 From: Xnoe Date: Sun, 21 Nov 2021 16:56:50 +0000 Subject: [PATCH] Preparations towards context switching --- Makefile | 2 +- src/kernel/global.cpp | 3 ++- src/kernel/global.h | 4 ++++ src/kernel/kernel.cpp | 17 ++++++++++++++--- src/kernel/kernel.h | 10 +++++++++- src/kernel/kmain.cpp | 6 +++--- 6 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 7614b83..116f681 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ run: disk.img qemu-system-x86_64 disk.img debug: disk.img - qemu-system-x86_64 -s -S disk.img & gdb --command=gdbscript + qemu-system-x86_64 -s -S -no-reboot -no-shutdown disk.img & gdb --command=gdbscript disk.img: clean prepare build/boot/boot.bin build/boot_stage2/boot.bin $(DISK_IMG_FILES) dd if=/dev/zero of=disk.img count=43 bs=100k diff --git a/src/kernel/global.cpp b/src/kernel/global.cpp index b1096d0..4d42b26 100644 --- a/src/kernel/global.cpp +++ b/src/kernel/global.cpp @@ -1,7 +1,8 @@ -#include "memory.h" +#include "global.h" namespace Global { Allocator* allocator = 0; + Kernel* kernel = 0; } void* operator new (uint32_t size) { diff --git a/src/kernel/global.h b/src/kernel/global.h index 03cdcc1..78fe794 100644 --- a/src/kernel/global.h +++ b/src/kernel/global.h @@ -1,10 +1,14 @@ #ifndef GLOBAL_H #define GLOBAL_H +#include "memory.h" + +class Kernel; class Allocator; namespace Global { extern Allocator* allocator; + extern Kernel* kernel; } void* operator new (uint32_t size); diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 777127f..4333bf2 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -1,9 +1,20 @@ #include "kernel.h" -Kernel* Kernel::kernel; - Kernel::Kernel(PageDirectory* page_directory, PageMap* phys, PageMap* virt, uint32_t virt_alloc_base) : Process(0, 0x8a000, page_directory, phys, virt, virt_alloc_base) { - this->kernel = this; + this->currentPID = 1; + Global::allocator = this; + Global::kernel = this; +} + +void Kernel::init_kernel() { + this->pid_map = new xnoe::hashtable(); +} + +Process* Kernel::createProcess() { + Process* p = new Process(currentPID); + this->pid_map->set(currentPID, p); + currentPID++; + return p; } \ No newline at end of file diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h index 720f219..aa98294 100644 --- a/src/kernel/kernel.h +++ b/src/kernel/kernel.h @@ -2,14 +2,22 @@ #define KERNEL_H #include "process.h" +#include "datatypes/hashtable.h" +#include "global.h" class Kernel : public Process { private: - static Kernel* kernel; + uint32_t currentPID; public: + xnoe::hashtable* pid_map; // Map of PIDs -> Process*s + uint32_t current_PID; Kernel(PageDirectory* page_directory, PageMap* phys, PageMap* virt, uint32_t virt_alloc_base); + + void init_kernel(); + + Process* createProcess(); }; #endif \ No newline at end of file diff --git a/src/kernel/kmain.cpp b/src/kernel/kmain.cpp index 1490a27..a5fbbf6 100644 --- a/src/kernel/kmain.cpp +++ b/src/kernel/kmain.cpp @@ -28,7 +28,7 @@ int main() { PageMap virt_pm(0xc0620000); Kernel kernel = Kernel(&kernel_pd, &phys_pm, &virt_pm, 0xc0000000); - Global::allocator = &kernel; + kernel.init_kernel(); Terminal* current_term; @@ -46,8 +46,8 @@ int main() { term->printf("Hello, World!\n\nWe are running XnoeOS Code in C++ now, Protected Mode has been achieved (as well as Virtual Memory / Paging!!!) and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~\n"); term->printf("KERNEL OK!\n"); - - Process* process = new Process(1); + + kernel.createProcess(); term->deactivate(); term2->activate();