Working kernel written on C and userspace-ready #1

Merged
sonya merged 61 commits from dev into main 2026-05-03 09:13:20 +00:00
4 changed files with 78 additions and 9 deletions
Showing only changes of commit 55335013a9 - Show all commits
+5 -1
View File
@@ -16,4 +16,8 @@ typedef struct {
VMMemoryRegion totalRAM; VMMemoryRegion totalRAM;
VMMemoryRegion reserved[kVMMaxReservedRegions]; VMMemoryRegion reserved[kVMMaxReservedRegions];
UInt32 reservedCount; UInt32 reservedCount;
} VMBootMemoryMap; } VMBootMemoryMap;
void PMMInitialize(VMBootMemoryMap* bootMap);
Pointer PMMAllocatePage();
void PMMFreePage(Address address);
+11 -7
View File
@@ -1,3 +1,4 @@
#include "Types.h"
#include <Arch/DTB.h> #include <Arch/DTB.h>
#include <OS/Panic.h> #include <OS/Panic.h>
#include <OS/Log.h> #include <OS/Log.h>
@@ -12,6 +13,11 @@ void DTBParse(Pointer dtb, VMBootMemoryMap* bootMap) {
OSPanic("Invalid DTB magic"); OSPanic("Invalid DTB magic");
} }
UInt32 index = bootMap->reservedCount;
bootMap->reserved[index].base = (Address)dtb;
bootMap->reserved[index].size = BytesSwap32(header->totalSize);
bootMap->reservedCount++;
UInt32 offStruct = BytesSwap32(header->offDtStruct); UInt32 offStruct = BytesSwap32(header->offDtStruct);
UInt32 offStrings = BytesSwap32(header->offDtStrings); UInt32 offStrings = BytesSwap32(header->offDtStrings);
@@ -51,23 +57,21 @@ void DTBParse(Pointer dtb, VMBootMemoryMap* bootMap) {
if (StringCompare(propertyName, "reg") == 0) { if (StringCompare(propertyName, "reg") == 0) {
if (StringStartsWith(currentNode, "memory")) { if (StringStartsWith(currentNode, "memory")) {
UInt32* cells = (UInt32*)structs; UInt32* cells = (UInt32*)structs;
UInt64 base = Merge32To64(BytesSwap32(cells[1]), BytesSwap32(cells[0])); Address base = Merge32To64(BytesSwap32(cells[1]), BytesSwap32(cells[0]));
UInt64 size = Merge32To64(BytesSwap32(cells[3]), BytesSwap32(cells[2])); Size size = Merge32To64(BytesSwap32(cells[3]), BytesSwap32(cells[2]));
OSLog("Main Memory: base=0x%x, size=0x%x\n", base, size);
bootMap->totalRAM.base = base; bootMap->totalRAM.base = base;
bootMap->totalRAM.size = size; bootMap->totalRAM.size = size;
} }
else if (inReservedMemory && currentDepth > reservedMemoryDepth) { else if (inReservedMemory && currentDepth > reservedMemoryDepth) {
UInt32* cells = (UInt32*)structs; UInt32* cells = (UInt32*)structs;
UInt64 base = Merge32To64(BytesSwap32(cells[1]), BytesSwap32(cells[0])); Address base = Merge32To64(BytesSwap32(cells[1]), BytesSwap32(cells[0]));
UInt64 size = Merge32To64(BytesSwap32(cells[3]), BytesSwap32(cells[2])); Size size = Merge32To64(BytesSwap32(cells[3]), BytesSwap32(cells[2]));
UInt32 index = bootMap->reservedCount; UInt32 index = bootMap->reservedCount;
bootMap->reserved[index].base = base; bootMap->reserved[index].base = base;
bootMap->reserved[index].size = size; bootMap->reserved[index].size = size;
bootMap->reservedCount++; bootMap->reservedCount++;
OSLog("Reserved Region (%s): base=0x%x, size=0x%x\n", currentNode, base, size);
} }
} }
+1 -1
View File
@@ -13,5 +13,5 @@ void KernelMain(Bootinfo* bootinfo) {
VMBootMemoryMap bootMap = {0}; VMBootMemoryMap bootMap = {0};
bootMap.reservedCount = 0; bootMap.reservedCount = 0;
DTBParse(bootinfo->dtb, &bootMap); DTBParse(bootinfo->dtb, &bootMap);
OSLog("Meow...\n"); PMMInitialize(&bootMap);
} }
+61
View File
@@ -1,4 +1,8 @@
#include <VM/PMM.h> #include <VM/PMM.h>
#include <Lib/String.h>
extern char _kernelStart[];
extern char _kernelEnd[];
static inline Size BitmapGetByteIndex(Address address) { static inline Size BitmapGetByteIndex(Address address) {
return (address / kVMPageSize) / kVMBlocksPerByte; return (address / kVMPageSize) / kVMBlocksPerByte;
@@ -20,3 +24,60 @@ static inline void BitmapUnset(MemoryPointer bitmap, Address address) {
bitmap[BitmapGetByteIndex(address)] &= ~(1U << BitmapGetBitOffset(address)); bitmap[BitmapGetByteIndex(address)] &= ~(1U << BitmapGetBitOffset(address));
} }
static MemoryPointer sPMMBitmap;
static Size sPMMBitmapSize;
static Size sPMMTotalPages;
void PMMInitialize(VMBootMemoryMap* bootMap) {
UInt32 vIndex = bootMap->reservedCount;
bootMap->reserved[vIndex].base = 0x0;
bootMap->reserved[vIndex].size = bootMap->totalRAM.base;
bootMap->reservedCount++;
UInt32 kIndex = bootMap->reservedCount;
bootMap->reserved[kIndex].base = (Address)_kernelStart;
bootMap->reserved[kIndex].size = (Address)_kernelEnd - (Address)_kernelStart;
bootMap->reservedCount++;
sPMMTotalPages = bootMap->totalRAM.size / kVMPageSize;
sPMMBitmapSize = sPMMTotalPages / kVMBlocksPerByte;
sPMMBitmap = (MemoryPointer)_kernelEnd;
StringSet(sPMMBitmap, 0, sPMMBitmapSize);
UInt32 bIndex = bootMap->reservedCount;
bootMap->reserved[bIndex].base = (Address)sPMMBitmap;
bootMap->reserved[bIndex].size = sPMMBitmapSize;
bootMap->reservedCount++;
for (Size i = 0; i < bootMap->reservedCount; i++) {
Address regionBase = bootMap->reserved[i].base;
Size regionSize = bootMap->reserved[i].size;
Size pagesToReserve = (regionSize + kVMPageSize - 1) / kVMPageSize;
for (Size p = 0; p < pagesToReserve; p++) {
Address pageAdress = regionBase + (p * kVMPageSize);
BitmapSet(sPMMBitmap, pageAdress);
}
}
}
Pointer PMMAllocatePage() {
for (Size i = 0; i < sPMMBitmapSize; i++) {
if (sPMMBitmap[i] == 0xFF) continue;
for (Size bit = 0; bit < kVMBlocksPerByte; bit++) {
if ((sPMMBitmap[i] & (1 << bit)) == 0) {
Address address = (i * kVMBlocksPerByte + bit) * kVMPageSize;
BitmapSet(sPMMBitmap, address);
return (Pointer)address;
}
}
}
return nullptr;
}
void PMMFreePage(Address address) {
BitmapUnset(sPMMBitmap, address);
}