Allocators
Allocators are responsible for creating and removing objects from the heap.
This blog is openly developed on GitHub. If you have any problems or questions, please open an issue there. You can also leave comments at the bottom. The complete source code for this post can be found in the post-09
branch.
Table of Contents
🔗Introduction
The previous post gave an introduction to the concept of paging. It motivated paging by comparing it with segmentation, explained how paging and page tables work, and then introduced the 4-level page table design of x86_64
. We found out that the bootloader already set up a page table hierarchy for our kernel, which means that our kernel already runs on virtual addresses. This improves safety since illegal memory accesses cause page fault exceptions instead of modifying arbitrary physical memory.
🔗Accessing Page Tables
🔗Identity Mapping
🔗Map at a Fixed Offset
🔗Map the Complete Physical Memory
🔗Temporary Mapping
🔗Recursive Page Tables
🔗Summary
In this post we learned about different techniques to access the physical frames of page tables, including identity mapping, mapping of the complete physical memory, temporary mapping, and recursive page tables. We chose to map the complete physical memory since it's simple, portable, and powerful.
We can't map the physical memory from our kernel without page table access, so we needed support from the bootloader. The bootloader
crate supports creating the required mapping through optional cargo features. It passes the required information to our kernel in the form of a &BootInfo
argument to our entry point function.
For our implementation, we first manually traversed the page tables to implement a translation function, and then used the MappedPageTable
type of the x86_64
crate. We also learned how to create new mappings in the page table and how to create the necessary FrameAllocator
on top of the memory map passed by the bootloader.
🔗What's next?
The next post will create a heap memory region for our kernel, which will allow us to allocate memory and use various collection types.