First step in implementing paging?
First step in implementing paging?
Hi, I am creating 64-bit x86-64 kernel and I need to implement paging. I think the UEFI firmware already set up the identity paging. As my kernel is small right now, I have attached my kernel binary to the same UEFI loader image (the `BOOTx64.EFI` file). What do I need to do first to start implementing paging?
-
- Member
- Posts: 219
- Joined: Tue Aug 26, 2008 11:24 am
- GitHub: https://github.com/sebihepp
Re: First step in implementing paging?
You definately need a physical page frame allocator, so you can allocate free pages to then fill them up with your PLM4, 3, 2 and 1.
The physical page frame allocator needs to be setup with the memory map you get from BIOS or UEFI.
The physical page frame allocator needs to be setup with the memory map you get from BIOS or UEFI.
Re: First step in implementing paging?
Thanks. I have already implemented the physical memory manager using the free stack data structure.
What should I do next? I think I should identity page my UEFI loader.
What should I do next? I think I should identity page my UEFI loader.
-
- Member
- Posts: 219
- Joined: Tue Aug 26, 2008 11:24 am
- GitHub: https://github.com/sebihepp
Re: First step in implementing paging?
So you have a physical memory management in place and the UEFI already set up paging for you.
As next step you could implement a function GetPhysicalAddress(void *VirtualAddress), that walks the page tables and gets the physical address for a given virtual one.
And then a function MapAddress(void *VirtualAddress, void *PhysicalAddress, <flags> ...) which maps a virtual address to a physical one.
Implement them only partly for the first time, like throwing an error when you would need to allocate a new page. Then test this algorith to the bone. Later you can expand the functions to also allocate new pages when needed. Then test it again.
Maybe a helper function retrieving the lowest level of paging for a virtual address is a good idea, because not everything might be mapped with 4KB pages. Some might use 2MB pages or even 1GB pages, that end on PML2/PML3.
It is also a good idea to check for support of 1GB pages and maybe even VA57 and implement all those function to be easily extended to work with these active.
As next step you could implement a function GetPhysicalAddress(void *VirtualAddress), that walks the page tables and gets the physical address for a given virtual one.
And then a function MapAddress(void *VirtualAddress, void *PhysicalAddress, <flags> ...) which maps a virtual address to a physical one.
Implement them only partly for the first time, like throwing an error when you would need to allocate a new page. Then test this algorith to the bone. Later you can expand the functions to also allocate new pages when needed. Then test it again.
Maybe a helper function retrieving the lowest level of paging for a virtual address is a good idea, because not everything might be mapped with 4KB pages. Some might use 2MB pages or even 1GB pages, that end on PML2/PML3.
It is also a good idea to check for support of 1GB pages and maybe even VA57 and implement all those function to be easily extended to work with these active.