This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is the 5th video in our virtual memory series. In the previous video, we learned about the page table and how the hardware performs a page table walk to do address translation. But merely finding the physical frame address in the page table is not sufficient for the hardware to do a memory access. It also needs to do certain additional checks to make sure that the access is valid. For example, it has to ensure that the page table mapping itself is valid (e.g., the page might have been swapped). Similarly, when doing a memory write, the hardware has to ensure that the page(s) are writable. All these checks are done during address translation by looking up additional metadata stored against the page table entry. In this video, we cover what all these metadata bits needed by the hardware are, what their purpose is, and how they are stored in the page table. In the next video, we will discuss demand paging. In the meantime, if you haven’t yet read the VM article, please do. And if you prefer reading it offline, get the ebook from 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 fifth video in the series and so far we have talked about the idea that the address space of a process consists of virtual addresses and the operating system maintains a mapping of those virtual addresses to physical addresses in the form of a page table. So it maps one page of the virtual memory to one physical frame
and one page is usually four kilobytes and it is mapped to a four kilobyte physical frame.
SPEAKER_01So whenever we try to access, do any memory access, the hardware uses the page table to translate that virtual memory address into a physical frame address and then it loops up the exact data within that physical frame. it does this with the help of a page table walk which is what we learned in the previous video this video is a logical continuation of the same topic because just looking up the page table and translating a virtual address to
a physical frame address is not sufficient for the hardware to do a proper memory access it also
SPEAKER_01needs to find other information about the page before doing the access so for example the hardware while it is doing the pasteable work it has to ensure that the pasteable mapping whether that is a valid mapping or not Because if it is an invalid mapping, then it should not be using that translation to access physical memory. So this can happen in the cases, for example, when the operating system performs a page reclamation.
SPEAKER_01So whenever there is memory pressure, the operating system has to reclaim some of the physical pages and reuse them for other processes. In those cases, it has to move those pages to to the disk. So, for example, if those are anonymous memory pages, so allocated using malloc or mmap as anonymous memory, then it has to write those pages to the swap and then reclaim them for another process. Or if those are file-backed pages,
then it may have to write those pages back to the file and then reuse those
SPEAKER_01physical pages for other purposes. So, in those cases, the page table mapping becomes invalid. So the hardware has to know whether those mappings that it is looking up, are those valid or not? Apart from that, there are other things the hardware may want to know. So for example, if you're trying to write to a page, it has to know whether the page has write access enabled on it or not, or whether it is a read-only page. So that information has to be available to the hardware.
Similarly when you are trying to execute a page so if a page contains code and you are trying to execute it so it should have the execution enabled on it or it should be marked as a code page so that has to be there so all this information is also stored in the page table and the hardware apart from looking up at the translation
SPEAKER_01apart from looking up the physical claim address it also looks up All this metadata in order to answer all these questions to ensure that the access is valid. This is obviously required for safe and secure security of the system. So let's talk about, so in this video we are going to talk about all this. So what are all these flags and metadata that is stored in the base table and how exactly is it stored? So how the hardware is able to find it?
SPEAKER_01So let's start by talking about all the flags that are present. So all the metadata itself. And then we'll talk about how is it stored in the page table.
SPEAKER_00So let's talk about all the bits. So one of the most important flags that is present in each page table mapping is
SPEAKER_01the present bit.
SPEAKER_00So it's called the present bit itself. so the present bit is essentially telling the hardware whether the mapping is valid or not so whether the mapping is present or not so when the hardware is trying to translate one virtual address it does a pasteable work and it reaches a certain level then it has to make sure whether the mapping whatever it is looking at whether that's a valid mapping so is the mapping itself present
or not so if the present bit is set that means the mapping is present and it is a valid mapping if
SPEAKER_01the present bit is unsaid that means Either the mapping was never made For example, when you allocate a new page For example, when you're doing malloc, the operating system may allocate a new virtual page to the processor, but it may not have mapped that virtual page to a physical frame yet. So the present bit would not be set on those pages. So that could be one case where the present bit is not set. Another example is the operating system has swapped those pages
to the disk,
SPEAKER_01so under memory pressure, so when doing page reclamation. so again in those cases the present bit will be unset which means whenever the operating system reclaims those pitches it goes and unsecs the present bit so that the hardware does not accidentally uses those mappings so that is another example so that is the use of the present bit so it tells the hardware whether the Mapping Is Valid Or Not Or The Mapping Is Present Or Not So If The Mapping Is Not Present So When The Hardware Is Trying To Translate It Does A
Page Table Walk And It Finds That Okay The Present Bit Is Not Set So What Does Happen What Happens So The Hardware Raises A Page Fault A Page Fault Is Sort Of An Exception That The Operating System Has To Handle So When The Page Fault Is Raised Then The Kernel comes into the picture so the kernel gets executed so kernel has to handle the page fault so the kernel then goes and sees which page you are
trying to access and what was the reason why its mapping was missing so the kernel would maintain that
SPEAKER_01information in the processes Data Structure. So whatever data structure it uses to maintain the virtual memory, it would store information about such pages. So for example, if a page has been swapped to the disk, then it would store in the processes data structures whether that page is swapped to the disk. If it is, then the kernel will load that page from the disk into the memory, into a fresh page frame,
SPEAKER_01and then update the page table mapping and point to the fresh page frame. Similarly, if it is a page which is not yet mapped to a physical frame yet, so for example, when this is what happens in demand paging, so you allocate more memory, the kernel gives you a new virtual page, but that page hasn't been mapped. So when a page fault happens, then the kernel gets executed and now the kernel sees,
SPEAKER_01okay, I allocated this page, but I haven't really allocated a physical frame. So in that case, the kernel will find a new physical frame that can be used, give it to the process, install it in its page table. After that, it re-executes the faulting instruction. So the next time the hardware does a page table work, it is able to find a new page, new frame. so all those things happen when the present bit is not set another the next thing
or the next metadata is a read or write permission so this essentially tells whether the page is writable or not so if the write this bit is not set so if it is
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...