This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is the fourth video in our virtual memory series based on the article/ebook I wrote. In the last few videos, we covered what virtual memory is, its size, and the address space layout of a process. In this video, we learn how the kernel stores virtual-to-physical address mappings in the form of a page table, what that looks like, and how the hardware performs an address translation by walking the page table. But instead of jumping directly to page tables, we derive the design from first principles like real system designers. We start from the problem statement of efficiently storing virtual address mappings and performing lookups efficiently, and from there, we iteratively arrive at the final solution that looks like modern-day page tables. I believe that this way, not only you would understand page tables better, but also develop the design chops needed to build solutions in other domains. In the next video, we will talk about protection bits in virtual pages. Till then, if you haven’t read the original article/book, I recommend checking that out. You can also get it in the form of a beautiful PDF for offline reading using the link below.
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_01Hey everyone, this is Abhinav. Welcome back to our series on virtual memory. This is the fourth video of this series. So far in the previous three videos, we have talked about what is virtual memory, the size of virtual memory and the address space layout of a process. In today's video, we are going to talk about how the kernel maintains the virtual to physical memory mappings and how the hardware looks up those mappings in order to do the address translation. So by the end of this video,
you will have a concrete understanding of page tables and how a page table walk
SPEAKER_01works. But we'll arrive at the page table solution by First Principles Reasoning. So we'll start from the problem statement about how the kernel maintains or stores the virtual to physical address mappings and how the hardware does translation. And we'll incrementally build a solution and finally get to page tables. The reason of taking this roundabout way is that so we
understand why exactly page tables look the way they look like so it's a very peculiar data structure and if most textbooks you know directly give you the structure of a page table but there is no explanation about you know how did someone think about it so I want to do that I
SPEAKER_01think this way you will also remember it much better and even if you forget you will know how to you know design a solution like that from first principles yourself so let's start so So basically, let's first start from the problem statement. So the address space of a process consists of virtual addresses and whenever the process wants to read or write any of its memory,
it will execute an instruction and the instruction will consist of the virtual
SPEAKER_01address of the memory location. Now, when the hardware is executing that instruction, it has to know the physical address. So the virtual address, you know, is useless for the hardware. So the hardware has to ask the kernel, what is the physical address to which this virtual address is mapped? And the kernel has the answer in the form of, you know, however the way it stores the mapping. So basically, the hardware has to go and perform a lookup of those mappings.
SPEAKER_01So that is the problem statement. So how the kernel stores the mappings? If we are the kernel designer, how can we store those mappings? So when we are trying to solve this problem, we have two constraints for which we have to optimize.
SPEAKER_00So one is memory efficiency. which means how frugal we are in storing those mappings because the operating system has hundreds to thousands of processes concurrently or parallelly and each
SPEAKER_01process will have its own mappings. the operating system must not waste too much memory in storing these mappings itself because then there will not be much memory left for the processes to use because all these mappings themselves have to be stored in memory somewhere and the
SPEAKER_00second constraint is lookup performance so when the hardware is trying to do a translation it has to look up these mappings and those lookups have to be really fast because majority of software is basically accessing memory so basically majority of instructions are doing memory access so
if and every memory access may require a translation and if those translations are
SPEAKER_01going to be expensive because the hardware is you know the kernel is not smart enough to store it in a way so that lookups are fast then everything will become slow so every process will be extremely slow so lookups have to be really really fast of these mappings so those are the two constraints we have to optimize for when we are trying to if we are the kernel designer and we have to solve this problem of storing these mappings of virtual addresses so
SPEAKER_01Now, so now we have our objective. So we have the problem statement that we want to store the mapping of virtual to physical addresses and we want to store them as memory efficiently as possible, which means storing as little as memory as possible and at the same time having fast flip-ups. So if we think about it, What is the most obvious way of doing this? So, the most obvious way is that,
SPEAKER_01let's say, we simply map every virtual address to a physical address. That is the simplest way to do that. So, whenever the kernel allocates some memory for the process, it simply stores the mapping itself. I asked or the process asked the kernel I want one byte of memory and the kernel gave me virtual address 1023 and let's say this 1023 virtual
address is mapped to physical address 60 so one option is that the kernel simply stores these mappings
SPEAKER_00as it is right so in this case how can the hardware do the lookup so the lookups have to be passed so one option is that kernel stores these mappings of every virtual address in the form of an array so and every index you have one index for every virtual address
so virtual
SPEAKER_01addresses go from 0 to to the size of virtual memory. So, in our case, on x8664, we have 2 to the power 48 virtual addresses because one virtual address is 48 bit
SPEAKER_00filed. So, this array has to go from 0 2 to the power 48 and every entry every index is mapping one virtual address at that index to a corresponding physical address so we'll need such a large array but if we are able to do this the lookups will be fast because lookups are basically constant time so in order to do one translation you always have
to do one memory
SPEAKER_01access within this array so lookups are passed but you can see that the memory efficiency is really bad because we will create an array of size so this array will add 2 to the power 48 entries and every entry will be 8 bytes wide so you will need 8 into 2 to the power
SPEAKER_0048 bytes to store the mapping themselves which is extremely huge I mean it's impractical 2 to about 48 is 256 terabytes and on top of that you need 8 times more memory so it's basically impractical to use this solution so we need to optimize the lookups are good if you were able to do this lookups were ideal we were able to have constant time
lookups because no
SPEAKER_01matter how large How much memory a processor is using, you always need to do one memory access to do the translation. So, lookups are ideal, but memory efficiency was really bad. We need to optimize. Now, whenever we think about optimizing, there is this idea that you should optimize for the common patterns that exist in your problem domain. because you can give a fast path for that.
So you have to think about what are the common patterns and then optimize for those.
SPEAKER_01So when we think about memory, one of the common access pattern is that we have a spatial locality. So basically, which means that whenever I'm trying to, for example, access one field of an object, it is very likely that I'll also access the nearby fields within the same object. So, for example, if I have an object like a pixel, a pixel would have maybe three fields, x, y, z, or maybe x and y.
SPEAKER_01So, if I'm accessing x, it is very likely that I'll also access y in a very short, you know, near future. Similarly, if I'm accessing arrays, so I'll access, traverse the arrays sequentially, so I'll access A0 to A-N. I'll do that sequentially. So these are the most common access patterns. Even code execution happens sequentially. So the instructions of our programs are stored contiguously.
SPEAKER_01And the normal flow is that you execute them one after another. So you have sequential pattern there as well. So if we can take advantage of this pattern, maybe we can reduce the overhead that is there for storing the mappings. So, how can we do that? So, basically, the optimization that we want here is that if I'm translating, if I'm doing one translation for accessing the first element of an array,
SPEAKER_01I shouldn't have to do, you know, for every other element of the array, I don't want to do, you know, translations again and again. A single translation should be able to serve me So, you know, for the majority of, you know, for the foreseeable future, you know, whatever accessing I'm doing within the array, I should be able to do, I shouldn't have to do more translations. So, a single translation should be able to serve me for a long time.
SPEAKER_01So, that is the idea because it is very likely we know that if I'm accessing one element of the array, I'll access others very soon. So I don't want to do translation again and again So how can we apply that to our problem here So the solution or the application is that instead of mapping one byte at a time So instead of mapping one byte of virtual address to one byte of physical address We map larger chunks of virtual memory to larger chunks
of physical memory
SPEAKER_00So this is done by mapping one page of virtual memory to one frame of physical memory so this is virtual and this is physical so a page is a larger contiguous chunk of virtual memory and a frame is a large chunk of physical memory and the
size of the page and
SPEAKER_01frame has to be same because We are mapping one same sized chunk of virtual memory to a same size chunk of physical memory. But now we don't have to maintain, you know, obviously you can think that, you know, that you can arrive at the conclusion that this will cut down the number of mappings we have to maintain. So earlier we needed 2 to the power 48 number of mappings if you were mapping every byte. Now we have to map one page at a time. So depending on the page size, we'll cut down the number of mappings we have to store.
SPEAKER_01So what is the size of a page? And there are various answers to that. There is no right or wrong answer. in modern systems in most operating systems and hardware so on x86-64 in Linux for example a page a virtual memory page is 4KB but you could also ask why not 1KB why not 2KB why not larger sizes like 8KB or 16KB or even 2MB so
one answer is that it
SPEAKER_01has been you know standard for a very long time in x86 world so somewhere in the 80s the 80386 processor came around which was a 32-bit processor with virtual memory and at that point the hardware designers decided that 4kb pages and since then it has become a standard but there are trade-offs it's not that 4kb is some sort of ideal answer
SPEAKER_00So there are two trade-offs with different page sizes. So one is number of mappings and second is internal wastage.
So, the larger the page size, the lesser the number of mappings that you have to store.
SPEAKER_01So, for example, if you use a page size of 1MB, then obviously you need very few number of pages to cover the entire virtual address space. So, number of mappings goes down. If you use a very small page size, for example, 1byte is the... Pathological Case. So if you have 1 byte pages, then you will need 2 to the power 48 mappings.
SPEAKER_01When you have 4 kb pages, then you need 2 to the power 36 pages. So the number of pages is 2 to the power 36 if you want to cover the entire address space. So there is that trade-off. So if you use larger page size, the number of mappings goes down, which means you can store the page table or the mappings more efficiently. But larger page sizes means that there might be a lot of internal wastage within a page. So for example, if you set your page size to be 1 MB and which
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we learned about copy-on-write (CoW) and understood its mechanics by digging inside the mmap pathway in the...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we discussed demand paging. Now, let’s move towards an even more interesting topic: copy-on-write (CoW). Just...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the previous video in our virtual memory series, we discussed demand paging. But that was mostly theoretical, explaining how...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com Welcome back to the video series on virtual memory based on my article on the same topic. It is also available in ebook format on...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is a short aside in our virtual memory series. We have talked extensively about the size of a virtual address and the virtual...