WIP: modules

This commit is contained in:
karina
2026-05-03 15:49:40 +04:00
parent 2c7396353c
commit 08248e3f3c
13 changed files with 539 additions and 62 deletions
+4
View File
@@ -18,6 +18,10 @@ Address ExceptionsHandler(ExceptionsContext* frame, ExceptionsType type) {
return SchedulerNext((Address)frame);
}
}
if (class == 0x15 && syndrome == 0) {
OSPanic("Wow! We are inside EL0! Syscall caught successfully! :D");
}
}
OSPanicException(frame);
}
+7
View File
@@ -13,6 +13,7 @@
#include <OS/Log.h>
#include <OS/Panic.h>
#include <OS/Scheduler.h>
#include <OS/Modules.h>
void KernelMain(Bootinfo* bootinfo) {
OSLog("Kernel started.\n");
@@ -26,6 +27,7 @@ void KernelMain(Bootinfo* bootinfo) {
DTBParse(bootinfo->dtb, &bootMap);
PMMInitialize(&bootMap);
VMMInitialize(&bootMap, bootinfo);
bootinfo = (Bootinfo*)VMPhysToHHDM((Address)bootinfo);
SerialUpdate(VMPhysToHHDM(bootMap.UART.base));
HeapInitialize();
@@ -37,5 +39,10 @@ void KernelMain(Bootinfo* bootinfo) {
CPUEnableInterrupts();
SchedulerInitialize();
for (UInt32 i = 0; i < bootinfo->moduleCount; i++) {
ModuleLoad(&bootinfo->modules[i]); // TODO: make some sort of priority of loading modules
// like init first then uart then fb ...
}
OSLog("Kernel initialized.\n");
}
+48
View File
@@ -0,0 +1,48 @@
#include <OS/Modules.h>
#include <OS/Scheduler.h>
#include <OS/Log.h>
#include <VM/Heap.h>
#include <VM/VMM.h>
#include <Lib/String.h>
#include <Arch/CPU.h>
#include "../Common/bootinfo.h"
void ModuleLoad(BootModule* module) {
OSProcess* userProc = HeapAllocate(sizeof(OSProcess));
MemorySet(userProc, 0, sizeof(OSProcess));
userProc->id = 1; // TODO: id gen
userProc->state = OSTaskStateRunning;
StringCopy(userProc->name, module->name);
Address userL0Phys = (Address)PMMAllocatePage();
Address* userL0Virt = (Address*)VMPhysToHHDM(userL0Phys);
MemorySet(userL0Virt, 0, kVMPageSize);
userProc->l0Table = userL0Virt;
Address physBase = module->physicalBase;
Address virtBase = module->virtualBase;
Size modSize = module->size;
UInt64 codeFlags = kPTENormalMem | kPTEUser | kPTEAccessRW | kPTEPrivNX; // TODO: kill RWX
for (Size i = 0; i < modSize; i += kVMPageSize) {
VMMMapPage((Address*)userL0Phys, physBase + i, virtBase + i, codeFlags);
}
CPUCleanAndInvalidateCode((void*)VMPhysToHHDM(physBase), modSize);
Address stackPhys = (Address)PMMAllocatePage();
Address userStackVirt = 0x80000000;
UInt64 stackFlags = kPTENormalMem | kPTEUser | kPTEAccessRW | kPTEPrivNX | kPTEUserNX;
VMMMapPage((Address*)userL0Phys, stackPhys, userStackVirt, stackFlags);
void (*userEntryPoint)() = (void(*)())module->entry;
if ((Address)userEntryPoint == 0x0) {
OSLog("Skipping module %s: entry point is 0x0.\n", module->name);
return;
}
SchedulerSpawn(userEntryPoint, userProc, true, userStackVirt + kVMPageSize);
}