1 / 10

Lecture 20: Virtual Memory

Lecture 20: Virtual Memory. Soon Tee Teoh CS 147. Virtual Memory. We allow programs to use a virtual memory space much larger than the physical memory space. Some of this “memory” is actually stored on disk. But this is invisible to the program.

palila
Download Presentation

Lecture 20: Virtual Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 20: Virtual Memory Soon Tee Teoh CS 147

  2. Virtual Memory • We allow programs to use a virtual memory space much larger than the physical memory space. • Some of this “memory” is actually stored on disk. But this is invisible to the program. • Virtual memory also allows different programs to access the same actual memory using different virtual addresses. • The Operating System (OS) manages virtual memory. The hardware provides support.

  3. Virtual Address to Physical Address Translation • We consider a unit of memory called page. A page is typically rather large, for example 4 KB ( 4096 bytes). • A page is the unit of memory that is transferred between disk and main memory. A virtual page is either in memory or on disk. • Example: • 32-bit virtual address space • Pages that are 4 KB large (4 KB = 212 bytes) • 16 MB main memory (16 MB = 224 bytes) 31 12 11 0 Virtual Address Virtual page number Page offset 23 12 11 0 Physical Address Physical page number Page offset

  4. Page number and page offset • A virtual address is therefore split into two parts. The more significant part gives the page number. The less significant part gives the address of the byte within the page. The less significant part is also called the “page offset.” • The number of bits allocated to the page offset should be able to address every byte in the page. Therefore, in our example. Since we have 4 KB = 212 byte page tables, we need 12 bits for page offset. • Likewise for the physical address.

  5. Page Table • A page table is used to map virtual page number to physical page number. • Suppose that a page table is stored in main memory. • Then the virtual page number is used to index the page table. • The content of the page table entry at that index is taken to be the physical page number (assuming that this page happens to be in main memory). • The physical page number is appended to the page offset (from the virtual address) to form the physical address. • Suppose that the page is not in main memory, then the entry in the page table will have a 0 Valid bit. In this case, the virtual memory management system (in software) is used to locate the page on disk.

  6. Directory Page • Suppose that each entry (mapping) in the page table requires 1 word (or 4 bytes). • There are 220 virtual pages (in our example), so if a single page table were to keep all the mappings (address translations), it would require 222 bytes (or 4 MB). • Considering that our main memory is only 16 MB, the page table would take up 25% of the main memory! • Instead, we have many smaller page tables, each containing a subset of the address translations. For convenience, we set each page table to be the size of one page (which is 4 KB) • The directory page provides the mappings to locate the appropriate page table for a particular address translation. • Thus, the virtual page number is split into two parts, the more significant part is the directory offset, and the less significant part is the page offset. • The directory offset is used to index the directory page to find the location of the page table containing the desired translation for this address. • The page offset is used to index the page table to find the physical page number. • Then, as before, the physical page number is used to locate the beginning of the physical page in memory, and the page offset is used to locate the exact byte within this page corresponding to the memory access.

  7. How to locate physical data from virtual address • A page table is 4 KB large, and each translation entry in the page table is 1 word (or 4 bytes) large. Therefore, a single page table can contain 1,024 (or 210) entries. • Therefore, the least significant 10 bits of the virtual page number becomes the page table offset, and the rest of the bits become the directory offset. Virtual page number 31 22 21 12 11 0 Virtual address Directory offset Page table offset Page offset Directory page Physical memory Page Table Page table page number Page Table Physical page number Physical byte referenced by virtual address Page Table Page Table

  8. Translation Look-aside Buffer • In the previous scheme, for every memory access, the MMU has to access the memory 3 times: • 1. Read from the directory page • 2. Read from the page table • 3. Read the byte from memory (if we have a cache, then this step 3 just needs to be a cache access) • This is too inefficient. • Solution: We have a cache for recently used virtual address to physical address translations. • This cache is called the Translation Look-aside Buffer (TLB) TLB entry V D Virtual page number Physical page number * V is the valid bit, and D is the dirty bit

  9. Translation Look-aside Buffer • The TLB tends to be fully associative. • The TLB uses write-back. • Therefore, needs a dirty bit to indicate if the page is dirty • This is because a TLB miss is very costly, and is to be avoided.

  10. Best case and Worst case • CPU requests a virtual address • Best case: The virtual page number of the address matches a virtual page number in the TLB, and the TLB entry also has the valid bit set to 1. The physical page number in the entry is appended to the page offset to get the physical address. This physical address is used to access the L1 cache, and we get a cache hit. • Total number of main memory accesses: 0 • Total number of disk accesses: 0 • Worst case: TLB miss, therefore access main memory to read the directory page, and page table. Page table happens to be on disk, so load page table from disk. When page table is loaded to memory, index the page table to find the appropriate entry. The entry in the page table has a 0 valid bit, indicating that the virtual address requested is on a page which is on the disk. Load the page from disk to memory, and read the requested data. • Total number of memory accesses: 3 (read directory page, load page table, load page) • Total number of disk accesses: 2 (load page table, load page) • Even worse case: Page is dirty, have to write back to disk. Cache is write-back. Cache entry is dirty, have to write back to main memory.

More Related