Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
BORA NIKOLIC
Hello, and welcome back to our operating system and virtual memory module. So far we have figured out how the virtual memory systems work.
And we have seen the role of translation lookaside buffers, or TLBs, in accelerating the translation from virtual addresses to physical addresses.
All that is left now is to just implement that in our pipeline. So let's do that.
So it actually could be left as an exercise to the listener, but we will nevertheless go ahead and walk through the implementation. So here is our five-stage pipeline that we have seen before.
We obviously see one modification that is in here. It is insertion of two TLBs in front of the instruction cache and the data cache.
Because we remember that TLBs need to precede the cache. Our pattern counter is going to issue virtual addresses.
TLB is going to translate these virtual addresses into the physical addresses before they go into the cache.
Similarly, our data memory references are going to be virtual before they reach the TLB. The TLB is going to translate them to the physical ones.
Now there are several events that these TLBs also need to alert us to.
Whether we have a TLB miss, was there a page vault, and is there a protection violation. So how do we handle those? First, let's take a look at TLB misses.
Handling a TLB miss needs a hardware or software mechanism to essentially walk the page tables and update the TLB. In most current machines, it is done in hardware.
And it can be done fairly quickly. Essentially what we are looking at is an implementation of a straightforward, perhaps not so simple state machine that is going to do that.
On the other hand, handling a page fault needs a precise trap. And we have seen how we implement those.
So if there is a page fault, we need to stop that and cancel that loader store instruction, because it is going to take forever.
So we will perform a precise trap, cancel it, and call the operating system. The operating system will do something else, most likely a context switch.
So once when the page is available in DRAM, we'll resume execution from the exact same instruction.
Finally, if there is a protection violation, generally, OS needs to know about that, and would typically abort the current process that made the protection violation.
Let's take a look a bit at the practical implementation here. So we can take our standard five-stage pipeline that we have already worked with.
And what we need to do is add our TLBs. Now, one thing to notice here is we have added a memory controller.
The memory controller works with the two caches, the instruction cache and the data cache.
It is essentially a chunk of digital logic that translates things from the processor speak to the DRAM speak.
So it is a pretty large digital system that essentially knows how to work with the DRAM. It knows how to refresh it.
It knows how to issue reads and writes, and how to do bursts, and so on.
This is not something that we are going to cover in 61C or actually anywhere in our curriculum.
This is something that commonly is a piece of IP, intellectual property, to buy on the market to integrate in your product, unless you're a really big company that has their own memory controllers.
But the things that do need to exist in our processor is the support for handling page misses.
So if there is a miss in a TLB, what we need to do is we need to walk the page tables. And that is typically done as a hardware state machine.
It starts from the page table base register that is set for every process that is being executed and the page table walker goes and updates the TLBs.
As we have seen, we essentially see in the process of that when it is done correctly, we see a translation from virtual addresses to the physical addresses at appropriate locations.
A few other things, we basically need to signal if we encounter a page fault, or a protection violation, either in the instructions or in the data TLB.
So that's basically it. To put it all together, here is how the address translation looks like in the practical system.
And we are outlining here things that are done in hardware, or in software, or can be done in either of those. Most of the time nowadays these things are done in hardware.
That can be done either in hardware and software. That's why we made them more like these lime greenish than orangish.
So when we start with the virtual address, we're going to go through a TLB lookup. Most of the time, 99.9-something time, you're going to have a hit.
If we have a hit, we need to check those bits that are in our TLB, and whether we are permitted to do the operation that we wanted to do, read, write, or execute.
If we are, we [INAUDIBLE] physical address, and that goes to the cache. And we proceed further.
If we are denied, we have a protection fault that is handled in software by the operating system, and generally the user sees a segfault error.
Now, going back to a TLB lookup, if we have a TLB miss, we need to perform a page table walk. That can be done in software or hardware.
But generally now is done in hardware. And if the page is in the memory, we'll update the TLB and carry on. That happens quickly. A few clock cycles, and you're good.
If the page is not in the memory, it's in the disk. We are going to issue a page fault.
And let the OS load the page, and do something else in the meantime, while that page is coming from the disk. And that's it. So a few other summary points here.
Modern virtual memory systems are there to provide an illusion of a large, private, uniform store.
And we have seen how we do that, how many of these mechanisms go together to make sure that the data always looks like it's right there, that it's within one cycle, even though it could be on Pluto.
So there are several other things that are implemented along the way, protection and privacy that several users and processes can co-exist in the processor.
And each one of them is going to have their private address space. We do that through demand paging that provides the ability to run programs larger than the primary memory.
And that is also very useful, because it hides differences in machine configurations. People can have different hardware and still run the same software.
The price for doing that is in the memory translation. And what do we do? We use TLBs to accelerate that.
And they work seamlessly with the caches to make our machines look like they're infinitely fast and work with infinitely large memory.
Finally, now we actually know how to define what is happening in a context switch. So how does the processor run many programs at once?
It uses context switches.
They are typically set by the timer, or it could be done by some external event, like a page fault. On the context switch, the operating system is going to change the internal state of the processor to switch between the processes.
It has to save all the registers, including the PC, and change that value in the supervisor table-based register, or that SPTBR such that we can point to a different set of page tables.
What happens to the TLB? Well, it's invalid. It corresponds to the old process. So it needs to be updated.
And that's it. After this, we are going to evaluate the performance of our virtual memory system and wrap up with the module. Move after that to I/O. See you after our quick break.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.