First step in implementing paging?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
osdev199
Posts: 19
Joined: Tue Apr 16, 2024 9:50 am

First step in implementing paging?

Post by osdev199 »

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?
sebihepp
Member
Member
Posts: 219
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: First step in implementing paging?

Post by sebihepp »

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.
osdev199
Posts: 19
Joined: Tue Apr 16, 2024 9:50 am

Re: First step in implementing paging?

Post by osdev199 »

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.
sebihepp
Member
Member
Posts: 219
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: First step in implementing paging?

Post by sebihepp »

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.
Post Reply