Virtual memory handling 1:1 mapping vs Recursion
Posted: Sun May 09, 2021 12:12 pm
I'm starting to implement the foundations of the virtual memory manager for my kernel, and now i need to decide how the kernel should access data in the Page Directories/table.
I knew about the recursion technique, implemented it in the past and probably should not take long to implement it again. But searching on alternatives i also saw that many peoples decide do do a 1-1 mapping of physical memory in a reserved address space (i.e. somewhere above 0xffffffff8000000).
Now the idea looks appealing to me so i was trying to read more about it. Found this blog post that explains different techniques pretty well: https://os.phil-opp.com/paging-implementation/, and according to the post:
While on the other hand disadvantages of recursion are:
So here is my question: Why 90% of the tutorials and 90% of amateur oses are using recursion instead of different techniques that looks even easier to understand (it took me a while to grab the logic behind recursion).
It has some hidden advantages?
Apparently even linux is mapping the whole phyisical memory on the ram:
full mapping available here: https://www.kernel.org/doc/Documentatio ... _64/mm.txt
I can't see many disadvantages of the memory mapping mode, if there any, can one can help me to choose? What are the cons or pros of both?
Is there something that is going to be more complicated to implement chosing one or another in the future?
Thanks to everyone who help!
I knew about the recursion technique, implemented it in the past and probably should not take long to implement it again. But searching on alternatives i also saw that many peoples decide do do a 1-1 mapping of physical memory in a reserved address space (i.e. somewhere above 0xffffffff8000000).
Now the idea looks appealing to me so i was trying to read more about it. Found this blog post that explains different techniques pretty well: https://os.phil-opp.com/paging-implementation/, and according to the post:
So apparently on a modern computer memory space shouldn't be a problem, and i also suppose that in a multitasking environment, i can reuse the same variables that contains directory/tables of the physical memory mapping on every process, since they are kind of common between all processes, i need to do the mapping only once at boot time. Correct?This approach allows our kernel to access arbitrary physical memory, including page table frames of other address spaces. The reserved virtual memory range has the same size as before, with the difference that it no longer contains unmapped pages.
The disadvantage of this approach is that additional page tables are needed for storing the mapping of the physical memory. These page tables need to be stored somewhere, so they use up a part of physical memory, which can be a problem on devices with a small amount of memory.
While on the other hand disadvantages of recursion are:
I don't think that the first one is a real problem, but maybe the last one is the more important, i'm not planning to support multiple archtectures, but if one day i or someone wants to try to port the kernel on another achitecture it could cause taht more code needs to be rewritter.
- It occupies a large amount of virtual memory (512GiB). This isn't a big problem in the large 48-bit address space, but it might lead to suboptimal cache behavior.
- It only allows accessing the currently active address space easily. Accessing other address spaces is still possible by changing the recursive entry, but a temporary mapping is required for switching back. We described how to do this in the (outdated) Remap The Kernel post.
- It heavily relies on the page table format of x86 and might not work on other architectures.
So here is my question: Why 90% of the tutorials and 90% of amateur oses are using recursion instead of different techniques that looks even easier to understand (it took me a while to grab the logic behind recursion).
It has some hidden advantages?
Apparently even linux is mapping the whole phyisical memory on the ram:
ffff888000000000 | -119.5 TB | ffffc87fffffffff | 64 TB | direct mapping of all physical memory (page_offset_base)
full mapping available here: https://www.kernel.org/doc/Documentatio ... _64/mm.txt
I can't see many disadvantages of the memory mapping mode, if there any, can one can help me to choose? What are the cons or pros of both?
Is there something that is going to be more complicated to implement chosing one or another in the future?
Thanks to everyone who help!