Docsity

Notes on Virtual Memory | Digital Computer Design | ECEN 5253, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECEN 5253 Digital Computer Design
Virtual Memory December 8, 2005 page 1 of 11
Virtual Memory
All of the addresses generated by the processor must be found in physical main memory at
the bottom of the cache memory hierarchy. Main memory size on current systems is usu-
ally limited (about 1 GB currently) by the expense of enlarging the memory. Two prob-
lems with a finite amount of main memory can be handled with a virtual memory system
design.
1. Programs can be bigger than the available main memory (no matter how big memory
is, someone will want more!). Without a virtual memory, programs must be divided
into pieces, called “segments,” that fit into physical memory. The segments are
designed by the programmer to be independent so that they do not have to be in mem-
ory at the same time. The programmer has to write code to bring in the segments from
disk into main memory, “overlaying” the segments as needed to fit into the available
memory space.
2. Memory must be shared between different processes (more than one process must be in
memory at the same time for context switching to be efficient). The operating system
assigns different parts of memory to each process. The program code for each process
must be “relocatable” to allow the operating system to put the process code anywhere at
any time.
To solve these problems in a manner transparent to users, the address generated by the
processor (the logical address or virtual address) is translated by the virtual memory into a
physical address that can be anywhere in main memory (fig. 7.19, p. 512). When there is
not enough main memory, disk storage is used to store main memory contents until
needed. In this way, each process can generate addresses for a “virtual machine” consist-
ing of the entire address space.
With the following correspondence in terminology,
cache virtual memory
miss page fault, address fault
block page, segment
virtual memory acts very much like
another level in the cache hierarchy with disk as the next lowest level memory (see fig.
7.22, p. 518).
physical memory length
segment #1 segment #2 segment #3unused
external fragmentation
page
#1
unused
internal fragmentation
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Notes on Virtual Memory | Digital Computer Design | ECEN 5253 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Virtual Memory

All of the addresses generated by the processor must be found in physical main memory at the bottom of the cache memory hierarchy. Main memory size on current systems is usu- ally limited (about 1 GB currently) by the expense of enlarging the memory. Two prob- lems with a finite amount of main memory can be handled with a virtual memory system design.

  1. Programs can be bigger than the available main memory (no matter how big memory is, someone will want more!). Without a virtual memory, programs must be divided into pieces, called “segments,” that fit into physical memory. The segments are designed by the programmer to be independent so that they do not have to be in mem- ory at the same time. The programmer has to write code to bring in the segments from disk into main memory, “overlaying” the segments as needed to fit into the available memory space.
  2. Memory must be shared between different processes (more than one process must be in memory at the same time for context switching to be efficient). The operating system assigns different parts of memory to each process. The program code for each process must be “relocatable” to allow the operating system to put the process code anywhere at any time.

To solve these problems in a manner transparent to users, the address generated by the processor (the logical address or virtual address) is translated by the virtual memory into a physical address that can be anywhere in main memory (fig. 7.19, p. 512). When there is not enough main memory, disk storage is used to store main memory contents until needed. In this way, each process can generate addresses for a “virtual machine” consist- ing of the entire address space.

With the following correspondence in terminology,

cache virtual memory miss page fault, address fault block page, segment

virtual memory acts very much like

another level in the cache hierarchy with disk as the next lowest level memory (see fig. 7.22, p. 518).

physical memory length

segment #1 unused segment #2 segment #

external fragmentation page

unused internal fragmentation

Information is transferred between disk and main memory either in variable sized “seg- ments” or fixed size “pages”. With segments, external fragmentation limits memory usage efficiency. With pages, internal fragmentation is a problem.

Program Segments. It has become common practice to divide program code into seg- ments by function. These segments are designed with “memory protection” in mind, rather then overlaying. Four common types of segments are:

  1. code (fetch an instruction)
  2. absolute (data accessed with absolute addressing mode)
  3. stack (data accessed with a stack base pointer register)
  4. data (data accessed any other way)

The point of these segments is to restrict the ways different segments can be used. It is taken to be an unintended error, for example, to write data into the program segment or fetch instructions from one of the data segments. Many systems support the user defining as many segments as he/she likes and also the user is allowed to control the access of each segment by setting protection bits for each segment.

Segmented Virtual Memory. Typical hardware support required for virtual memory with variable length segments is shown below.

virtual address^ segment offset

address fault

physical address

segment registers

base length prot

code segment base length prot base length prot base length prot

absolute segment stack segment data segment

protection fault

MUX MUX MUX

adder comparator^

protection logic

read/write control

Intel processors allow a much larger number of segments than the 4 shown above. The segment portion of the virtual address selects a segment descriptor from an area of mem- ory called the segment descriptor table. Segment descriptors take the place of the segment

For large memories, the page tables can get quite large. For example, if the page size is

8K bytes (DEC Alpha AXP 21064), then no must be 13 (8K = 2^13 ). If the address is 43

bits, then nv must be 30 (43 - 13), and there will be 2^30 = 1G PTE’s. The size of the PTE

depends on the maximum number of physical pages implemented in memory. Around 8 bytes is usually sufficient for the physical memory address and the protection bits. This gives

page table size = (no. of pages) x (PTE size) = 1G x 8 bytes =8 GB Thus, just the page table would be about as large or larger than a typical physical memory!

Multi-level Paging. Multi-level paging can reduce the total size of the page table. Con- sider the following three level paging scheme with the same numbers as the previous example. The original logical page number is divided into three fields, each with its own page tables. The page table register points to the level 1 page table. The level 1 and 2 page table entries contain the physical page number of the next level page table. The level 3 page table contains the physical page number of the desired memory location. The lowest

three bits of the PTE address are zero since each PTE is 2^3 = 8 bits in length.

level1 level2 offset

level

page table register

level 1 Page Table

level 2 Page Table

level 3 Page Table

physical address

logical address

The logical page address has been divided into three 10-bit fields so that the page tables for each of the three levels will be the same size.

level-1 PTE’s = # level-2 PTE’s = # level-3 PTE’s = 2^10 = 1K

PTE size = 8 bytes therefore

page table size = 1K x 8 bytes = 8KB

A page table fits exactly into one page. This simplifies the task of managing the page tables. A single page table is much smaller than the 8GB page table with single level pag- ing. Unfortunately, there is now more than one page table. There is one page table at the first level, but each level-1 PTE points to a different level-2 page table. There may be as

many as 2^10 level-2 page tables! For each level-2 PTE, there can be as many as 2^10 level- 3 page tables! The total memory taken up by page tables can be as much as

mem for page tables = (# tables) x (table size) = (1 + 2^10 + 2^10 x 2^10 ) x 8KB = 1M x 8KB = 8GB which does not save space! However, it is not necessary for all of the level-2 and level- page tables to be present if the program segments do not use all of the address space. For

example, a program can fit into a single level-3 page table if it is shorter than 2^10 x 2^13 = 8M bytes. Having a single level-1 page table, a single level-2 page table and a single level-3 page table would require 24KB which is acceptable. Although the maximum amount of memory is not reduced, clearly the average amount of memory used by page tables is reduced by using multi-level paging.

This design has several advantages.

  1. Page tables fit naturally into the memory page.
  2. The physical address is generated by concatenation, not addition, which is much faster.
  3. The physical address in the PTE is concatenated (not added as with segments) with a field in the virtual address to produce an address for the next level page table which allows the page tables to be anywhere in memory.
  4. The disk address can be stored in the PTE of a page not in memory (invalid page).
  5. There are protection and flag bits available for each page (including the page tables themselves). In addition to read, write and execute protection, additional flag bits usu- ally include a valid page bit which indicates whether the page is in memory (vl = 1) or on disk (vl = 0). Other flag bits might indicate whether the page is uncacheable (blocks from this page always generate a cache miss) or unpageable (cannot be swapped back out to disk while the process is running).
  1. Put the TLB in series with the data cache and store PA’s as the data cache tag (as in fig. 7.24, p. 525).

CPU

TLB

Data Cache

VPN offset

PPN offset

tag block word

VA

PA

PA tag advantages: there is a one to one correspondence between cache and main memory (a block in main memory is represented by only one block in cache). If data in cache is changed (CPU write), it is not necessary to worry about updating other blocks in cache that correspond to the same main memory location.

PA tag disadvantages: The cache must wait for the TLB to provide the physical address. This puts the TLB on the critical timing path that determines the processor clock period! This disadvantage can be overcome by putting the TLB in a separate pipeline stage of a superpipelined processor.

  1. Put the TLB and data cache in parallel, store VA’s in the data cache tag and use an RTB to remove alias VA’s from the data cache.

Data Cache

tag block word

CPU

TLB

VPN offset

PPN offset

VA

RTB

PA

comparator

stored

VA

replace

stored

VA

VA tag advantages: If the VA is used to address the cache, the cache does not wait for the TLB. The Reverse Translation Buffer (RTB) does not have a significant impact on mem- ory delay since the RTB is used only on cache miss.

VA tag disadvantages: Unfortunately, there is not necessarily a one to one correspondence between VA’s and PA’s. For example, the operating system and the current process may use different VA’s to refer to the same physical address space. The duplicate VA’s are called “aliases.” When a CPU write modifies data in the cache, data consistency requires that cache locations corresponding to all aliases must also be updated. One way to do this is to allow only one valid alias in cache at a time, and thus avoid having to update aliases. A reverse translation buffer (RTB) is used to stores the VA corresponding to each PA. When a PA is generated by the TLB on cache miss, the RTB is checked to see if another VA already corresponds to this PA. If so, then the old VA entry is invalidated before the new one is read into data cache. Unfortunately, the RTB would need an entry for each main memory location (each PA) which would be too large.

VA cache is not commonly used because of the difficulty of removing aliases.

Fully associative cache (12 locations)

virtual instr address [42:0] (^) instruc- virtual data address [42:0] tion bus [63:0]

data out bus [63:0]

data in bus [63:0]

CPU

ITLB

virtual page no. [42:13]

offset [12:0]

virtual page no. [42:13]

offset [12:0]

virtual tag [42:13]

phys page no. [33:13]

Direct mapped cache (256 locations)

ICACHE

phys tag [33:13]

data [255:0]

index [12:5]

word offset [4:3]

byte offset [2:0]

Fully associative cache (32 locations)

DTLB

virtual tag [42:13]

phys page no. [33:13]

index [12:5]

word offset [4:3]

byte offset [2:0]

Direct mapped cache (256 locations)

DCACHE

phys tag [33:13]

data [255:0]

mux mux

shift

Prefetch Buffer

tag [33:5]

data [255:0]

Fully associative cache (1 location) Delayed Write Buffer

tag [33:3]

data[63:0]

Fully associative cache

(1 location)

Write Buffer

tag [33:5]

data [255:0]

Fully associative cache (4 locations)

address [33:5]

data [255:0] off chip interface

Cache Controller. In the event of a cache miss or TLB miss, the processor must be stalled while the cache fill sequence takes place or the page tables are read. Even under normal conditions (TLB hit and cache hit), extra hardware must check for access privileges and that there is a valid page in memory (it might have been swapped out to disk). This work is done by the cache controller hardware.

The cache controller along with the on-chip cache is often called the Memory Manage- ment Unit (MMU). The MMU includes all of the hardware necessary to fool the proces- sor into thinking that it has a large, very fast memory. The MMU does this by stalling the processor when necessary while the cache controller in the MMU loads the TLB and caches.

The cache controller is usually implemented as a small state machine (Moore machine). A typical flow chart for design of the controller state machine is shown in fig. 7.25, p. 526. Part of the work can be done by the processor executing protected code in a special read only memory (ROM). To do this requires interrupting the processor as discussed in a pre- vious section.

Memory System Examples. A photograph of the AMD Opteron (same instruction set as Intel Pentium IV) is shown in fig. 7.33, p. 546. Note the huge L2 on chip cache, smaller L1 instruction and data caches and the relatively small area for the Execution Unit. Both of these processors are superscalar processors which will be discussed next semester.

The on chip TLB organization of the Intel Pentium IV and the AMD Opteron is compared in fig. 7.34, p. 547 and the on chip cache organization is compared in fig. 7.35, p. 548.

Finally, the on chip memory systems of several processors are compared in fig. 7.36, p.