/// <summary> /// Creates a new page directory using only physical memory (used in Init) /// </summary> /// <param name="flags">The flags</param> /// <returns>The page directory</returns> public static PageDirectory *CreateNewDirectoryPhysically(PageFlags flags) { // Allocate a new block of physical memory to store our physical page in PageDirectory *directory = (PageDirectory *)Heap.AlignedAlloc(0x1000, sizeof(PageDirectory)); directory->PhysicalDirectory = directory; if (directory == null) { Panic.DoPanic("directory == null"); } // Allocate the tables for (int i = 0; i < 1024; i++) { PageTable *table = (PageTable *)PhysicalMemoryManager.Alloc(); if (table == null) { Panic.DoPanic("table == null"); } Memory.Memclear(table, sizeof(PageTable)); // Note: At this point, virtual address == physical address due to identity mapping directory->PhysicalTables[i] = (int)table | (int)flags; directory->VirtualTables[i] = (int)table; } return(directory); }
/// <summary> /// Allocates a virtual address range /// </summary> /// <param name="size">The size</param> /// <returns>The pointer to the block</returns> public static void *AllocateVirtual(int size) { // Page align size uint sizeAligned = AlignUp((uint)size); // Allocate int free = bitmap.FindFirstFreeRange((int)(sizeAligned / 0x1000), true); int start = free * 0x1000; int end = (int)(start + sizeAligned); PageFlags flags = PageFlags.Present | PageFlags.Writable | PageFlags.UserMode; for (int address = start; address < end; address += 0x1000) { int phys = (int)PhysicalMemoryManager.Alloc(); MapPage(KernelDirectory, phys, address, flags); MapPage(CurrentDirectory, phys, address, flags); } // Clear the data before returning it for safety Memory.Memclear((void *)start, size); return((void *)start); }