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ả.
[MUSIC PLAYING] PROFESSOR: Hello, and welcome back to our OSM virtual memory module.
In the previous segment, we talked about the concept of a memory manager. Our memory manager has three roles, one to provide translation between the virtual addresses and the physical addresses.
Two, to provide protection between the processes so then they don't overrun each other in the memory.
And three, it provides a mechanism for swapping some chunks of data from the DRAM to the disk.
Let's take a look at how does this swapping actually work and how it is being performed.
Generally, it is a good idea to always move a constant amount of data, constant chunk of data, fixed chunk of data between the DRAM and the disk.
Remember, simplicity is what we strive for in computer architecture. So we don't want to have some variable amount of data it will be difficult to adjust.
We want to have a fixed amount of data that we are going to move between the memory and the disk.
And that chunk of data is going to be called a page.
In most modern operating systems it is called the page, so what we are dealing with is a concept of a page memory system. How large is the page?
Well, it shouldn't be very small, because we don't want to go too frequently to the disk because operations with disk are expensive in terms of time.
On the other hand, we don't want to have too large of a chunk that we would like to move.
Most modern operating systems pick page size of 4-KiB, 4 kilobytes.
And that also is conveniently a multiple of the minimum addressable block in hard disk drives, which is 512 bytes.
So for kilobits is the minimum amount of data, we can retrieve from a hard disk. So the page size is said to be 4 kilobytes, or 4-KiB.
Remember, RISC-V is a byte addressable ISA, so every byte within the page should be addressable.
How many bits do we need to address 4 kilobytes? 4,096 words? Well, we need 12 bits for that.
So if you have a 32-bit address space, we should split it into two parts in our virtual addresses.
The lower 12 bits will be used for addressing bits within the page and the upper 20 bits will be the addresses of the pages or the page numbers.
How many pages can we have? Well, whatever 20 bits gets us, which is two to the 20 or a million pages.
So 32-bit address space gives us a million pages and 4,096 bytes within each of the pages.
All right, so let's see how does this work with our memory manager.
Remember, our conceptual memory manager assigns some part of the memory to each of these processes.
Every single process has access to the full memory, but they generally don't use all of the memory.
Each process often has the big donut hole that is in between the heap and the stack that is empty unused memory.
In this concept here, it is shown that it is using continuous memory that is allocated in continuous memory chunks to each of the process, but in the practice, when we look at it, the page memory essentially shuffles these pages as it likes, as it is convenient to it.
So the address translation happens here through the page tables.
There are each process is assigned a page table to it that is managed by the operating system.
So all virtual addresses are essentially point to the page table. Page table entries are the physical addresses of the memory that is in DRAM.
So if this first process is the orange, or California Gold process that is using three pages, three 4,096 byte chunks, then these California Gold pages can be spread across the memory.
The yellow process may be using four pages that can be in their different locations in the memory, and finally, the blue process may be just using a blue, one blue page, and obviously, these pages don't need to be located consecutively in the memory.
It is up to the OS where does it find it convenient to put it. And not all of them need to be in DRAM, some of them may be swapped to the disk.
Again, these page tables are managed by the operating system.
Let's take a look at this first role of the memory manager. Memory manager here provides the memory address translation.
This memory translation is happening from the virtual address space to the physical address space, and it is done by retaining the offset, the offset stays untouched.
The page table entry is used as a lookup to the page table to produce the actual page number or the physical address of the page.
Now, notice that physical addresses may, but don't have to have more or fewer bits than the virtual addresses.
Remember, in my computer, virtual addresses are 48 bits, physical addresses are 39 bits. So let's take a look a little bit in a little bit more detail into this process of address translation.
So operating system keeps track of which process is active. When the process takes over the processor, its page table becomes active as well.
It is a part of the state that corresponds to that process. So then memory manager extracts the page number from the virtual address.
It takes the top 20 bits from the virtual address and retains the 12 bits and save the lower 12 bits.
So looks up that page table and three in the page table and computes the physical memory address from the sum of the page, address, and the offset, or actually concatenation of the page number, page address, and the 12 offset bits.
Okay, let's take a look at how it perform its second role, the protection. So assigning different pages in DRAM through processes keeps them from accessing each other's memory.
So this allows processes to be isolated from each other, and these page tables are not handled by the processes themselves, they are managed by the OS in the supervisory mode.
Now, you may ask, what happens if we need to share the data between two multiple processes? Well, that's possible.
There would be a bit that would flag that a particular page is shareable between the processes.
So the OS will assign the same physical page to two different virtual addresses in two different processes, and that will permit us to share data between the processes.
Okay, how do we make sure that we do not write over certain pages that should not be writable?
Well, generally, there would be a bit that indicates that the page is right protected. So we can do that with a single bit, that would be somewhere in this page table.
So page tables, in addition to these physical addresses as their entries, are going to have some flags single bits that will be indicated things something about the status of that page.
So for example, those that are write protected may have their bit set to 1.
So if you try to write to write protected page, that would draw an exception, and then the OS would be handling that exception.
We'll let you know about that. Okay, now, another important thing here.
We understand how does this whole translation happen and the protection, how is the protection implemented, but where are these page tables? Are they inside the processor?
Are they yet another separate chunk of memory? Well, let's think a little bit about that.
How many pages are there and how big are these pages?
So if you have a 32-bit virtual address with 4-KiB pages, a single page table will have 2 to the 20 entries, and each entry will be 4 bytes wide.
So this is 4 megabytes 4MiB.
That's not a lot for a laptop like this.
If a laptop has 4 gigabytes of memory, then that would be just 0.1% of the physical memory space, but that's typically just too much for a cache.
That's comparable to size of a cache. Cache would not be containing anything else other than the current page table.
So we can't store pages inside the process. If we can't store them in that cache, they are too big. They would have to reside in the memory. There is a consequence of that.
In order to perform a load or a store, we need to now make two trips to the memory.
We first need to get the page table from the memory, and then when we have that, when we have the actual physical address, we can perform the load or a store.
Well, that's inconvenient. Remember, accesses to the to the memory are expensive. These are compensated by the use of a cache.
Page tables, not the entire page tables, but parts of the page tables will be cached.
The frequently accessed entries in the page table will be cached, because our cache replacement policy is going to support that, so that is what is going to speed up.
So most of the time, we don't need to make two trips to the DRAM in order to load or store data. It is going to be residing in the cache.
So both our data and the page entries, page table entries, are going to be residing in the cache.
So just in a quick summary, our page tables are going to be stored in the memory.
So whenever we are referencing some data in the memory, we are going to first reference the page table and that page table is going to reference the actual page.
And that is going to happen for every process.
We're going to take a look in a little bit more detail of what else is in this page tables after the break. See you then.
Đă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.