Virtual allocator and paging design
Posted: Thu Nov 05, 2015 8:01 am
Up till now, my physical and virtual page allocators both used a bitmap. The space for the bitmaps was allocated before identity mapping the kernel (and the frambuffer video memory) by increasing the "end of kernel" address (and making sure it doesn't collide with a memory hole). So, basically I had
of static storage for the physical and virtual allocators.
During paging initialization, every mapped page was "excluded" from the virtual allocator bitmap. After paging was set up, all was set up for a basic kernel heap implementation, i.e.:
But then it hit me - why should the virtual allocator be responsible for finding free pages with it's own 4MiB bitmap, when the paging structures (PD's and PT's) already store this information?
The page table entries can be iterated in a similar fashion to bits in a bitmap. In addition, a 'next-free' variable could always store the address of the first unmapped virtual page. This way the virtual allocator just needs to ask for 'n' consecutive unmapped pages, allocate 'n' physical pages (I'm not sure whether it's advisable for physical pages to be consecutive too) and map all virtual pages to the physical ones.
I'm not too sure about this, especially since I haven't gotten to the point of processes and user-space management, and I have a feeling that once I get there I'll have to redesign this again. It seems like the general advice is to just implement Paging::map and rely on virtual and physical memory allocators to actually keep track of allocated space.
What's a good way to design the relationship between the physical allocator, the virtual allocator, the paging implementation and the kernel heap?
Code: Select all
end-of-physical-memory / 1024 / 8 + 0x100000000 / 1024 / 8
During paging initialization, every mapped page was "excluded" from the virtual allocator bitmap. After paging was set up, all was set up for a basic kernel heap implementation, i.e.:
Code: Select all
auto phys = PhysicalAllocator::allocate(pages);
auto virt = VirtualAllocator::allocate(pages);
Paging::map(virt, phys, pages, flags);
The page table entries can be iterated in a similar fashion to bits in a bitmap. In addition, a 'next-free' variable could always store the address of the first unmapped virtual page. This way the virtual allocator just needs to ask for 'n' consecutive unmapped pages, allocate 'n' physical pages (I'm not sure whether it's advisable for physical pages to be consecutive too) and map all virtual pages to the physical ones.
I'm not too sure about this, especially since I haven't gotten to the point of processes and user-space management, and I have a feeling that once I get there I'll have to redesign this again. It seems like the general advice is to just implement Paging::map and rely on virtual and physical memory allocators to actually keep track of allocated space.
What's a good way to design the relationship between the physical allocator, the virtual allocator, the paging implementation and the kernel heap?