Virtual Memory
In a memory hierarchy system, programs and data are first stored in auxiliary memory.
Portions of a program or data are brought into main memory as they are needed by the CPU.
Virtual memory is a concept used in some large computer systems that permit the user to
construct programs as though a large memory space were available, equal to the totality of
auxiliary memory. Each address that is referenced by the CPU goes through an address
mapping from the so-called virtual address to a physical address in main memory. Virtual
memory is used to give programmers the illusion that they have a very large memory at their
disposal, even though the computer actually has a relatively small main memory.
Address Space and Memory Space
An address used by a programmer will be called a virtual address, and the set address space
of such addresses the address space. An address in main memory is called a memory space
location or physical address. The set of such locations is called the memory space. Thus the
address space is the set of addresses generated by programs as they reference instructions
and data; the memory space consists of the actual main memory locations directly
addressable for processing. The address space is allowed to be larger than the memory space
in computers with virtual memory.
Consider a computer with a main-memory capacity of 32K words (K = 1024). Fifteen bits are
needed to specify a physical address in memory since 32K = 215. Suppose that the computer
has available auxiliary memory for storing 220 = 1024K words. Thus auxiliary memory has a
capacity for storing information equivalent to the capacity of 32 main memories. Denoting
the address space by N and the memory space by M, we then have for this example N = 1024K
and M = 32K.
In a multiprogram computer system, programs and data are transferred to and from
auxiliary memory and main memory based on demands imposed by the CPU. Suppose that
program 1 is currently being executed in the CPU. Program 1 and a portion of its associated
data are moved from auxiliary memory into main memory. Portions of programs and data
need not be in contiguous locations in memory since information is being moved in and out,
and empty spaces may be available in scattered locations in memory.
In the above example, the address field of an instruction code will consist of 20 bits but
physical memory addresses must be specified with only 15 bits. Thus CPU will reference
instructions and data with a 20-bit address, but the information at this address must be taken
from physical memory because access to auxiliary storage for individual words will be
prohibitively long.
A table is then needed, to map a virtual address of 20 bits to a physical address of 15 bits.
The mapping is a dynamic operation, which means that every address is translated
immediately as a word is referenced by CPU. The mapping table may be stored in a separate
memory or in main memory. In the first case, an additional memory unit is required as well
as one extra memory access time. In the second case, the table takes space from main
memory and two accesses to memory are required with the program running at half speed.
Address mapping using pages
The physical memory is broken down into groups of equal size called blocks, which may
range from 64 to 4096 words each. The term page refers to groups of address space of the
same size. For example, if a page or block consists of IK words, then, address space is divided
into 1024 pages and main memory is divided into 32 blocks. Although both a page and a
block are split into groups of IK words, a page refers to the organization of address space,
while a block refers to the organization of memory space.
Consider a computer with an address space of 8K and a memory space of 4K. If we split each
into groups of IK words we obtain eight pages and four blocks as shown in the following
figure. At any given time, up to four pages of address space may reside in main memory in
any one of the four blocks.
The mapping from address space to memory space is facilitated if each virtual address is
considered to be represented by two numbers: a page number address and a line within the
page. In a computer with 2P words per page, p bits are used to specify a line address and the
remaining high-order bits of the virtual address specify the page number. In the above
example figure, a virtual address has 13 bits. Since each page consists of 210 = 1024 words,
the highorder three bits of a virtual address will specify one of the eight pages and the low-
order 10 bits give the line address within the page. Here the line address in address space
and memory space is the same; the only mapping required is from a page number to a block
number.
The organization of the memory mapping table in a paged system is shown in the following
figure. The memory-page table consists of eight words, one for each page. The address in the
page table denotes the page number and the content of the word gives the block number
where that page is stored in main memory. The table shows that pages 1, 2, 5, and 6 are now
available in main memory in blocks 3, 0, 1, and 2, respectively. A presence bit in each location
indicates whether the page has been transferred from auxiliary memory into main memory.
A 0 in the presence bit indicates that this page is not available in main memory. The CPU
references a word in memory with a virtual address of 13 bits. The three high-order bits of
the virtual address specify a page number and also an address for the memory-page table.
The content of the word in the memory page table at the page number address is read out
into the memory table buffer register. If the presence bit is a 1, the block number thus read
is transferred to the two high-order bits of the main memory address register. The line
number from the virtual address is transferred into the 10 low-order bits of the memory
address register. A read signal to main memory transfers the content of the word to the main
memory buffer register ready to be used by the CPU. If the presence bit in the word read
from the page table is 0, it signifies that the content of the word referenced by the virtual
address does not reside in main memory. A call to the operating system is then generated to
fetch the required page from auxiliary memory and place it into main memory before
resuming computation.