feat(arm64): higher-half kernel, early MMU in boot, and VMM

This commit is contained in:
karina
2026-04-26 21:47:41 +04:00
parent 3a55665bd7
commit b56b55e4b3
14 changed files with 485 additions and 55 deletions
+18 -23
View File
@@ -1,19 +1,21 @@
#include <VM/PMM.h>
#include <Lib/String.h>
#include "../Common/bootinfo.h"
extern char _kernelStart[];
extern char _kernelEnd[];
static Address sPMMRamBase = 0;
static MemoryPointer sPMMBitmap;
static Size sPMMBitmapSize;
static Size sPMMTotalPages;
static inline Size BitmapGetByteIndex(Address address) {
return (address / kVMPageSize) / kVMBlocksPerByte;
return ((address - sPMMRamBase) / kVMPageSize) / kVMBlocksPerByte;
}
static inline UInt8 BitmapGetBitOffset(Address address) {
return (UInt8)((address / kVMPageSize) % kVMBlocksPerByte);
}
static inline Boolean BitmapTest(const MemoryPointer bitmap, Address address) {
return (bitmap[BitmapGetByteIndex(address)] & (1U << BitmapGetBitOffset(address))) != 0;
return (UInt8)(((address - sPMMRamBase) / kVMPageSize) % kVMBlocksPerByte);
}
static inline void BitmapSet(MemoryPointer bitmap, Address address) {
@@ -24,27 +26,18 @@ static inline void BitmapUnset(MemoryPointer bitmap, Address address) {
bitmap[BitmapGetByteIndex(address)] &= ~(1U << BitmapGetBitOffset(address));
}
static MemoryPointer sPMMBitmap;
static Size sPMMBitmapSize;
static Size sPMMTotalPages;
void PMMInitialize(VMBootMemoryMap* bootMap, Bootinfo* info) {
sPMMRamBase = bootMap->totalRAM.base;
sPMMTotalPages = bootMap->totalRAM.size / kVMPageSize;
sPMMBitmapSize = sPMMTotalPages / kVMBlocksPerByte;
sPMMBitmap = (MemoryPointer)_kernelEnd;
MemorySet(sPMMBitmap, 0, sPMMBitmapSize);
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;
@@ -58,7 +51,9 @@ void PMMInitialize(VMBootMemoryMap* bootMap) {
for (Size p = 0; p < pagesToReserve; p++) {
Address pageAdress = regionBase + (p * kVMPageSize);
BitmapSet(sPMMBitmap, pageAdress);
if (pageAdress >= sPMMRamBase && pageAdress < (sPMMRamBase + bootMap->totalRAM.size)) {
BitmapSet(sPMMBitmap, pageAdress);
}
}
}
}
@@ -68,7 +63,7 @@ Pointer PMMAllocatePage() {
if (sPMMBitmap[i] == 0xFF) continue;
for (Size bit = 0; bit < kVMBlocksPerByte; bit++) {
if ((sPMMBitmap[i] & (1 << bit)) == 0) {
Address address = (i * kVMBlocksPerByte + bit) * kVMPageSize;
Address address = sPMMRamBase + (i * kVMBlocksPerByte + bit) * kVMPageSize;
BitmapSet(sPMMBitmap, address);
return (Pointer)address;
}