4. INTRODUCTIONINTRODUCTION
In computing, In computing, virtual memoryvirtual memory
is a memory managementis a memory management
technique that is implementedtechnique that is implemented
using both hardware andusing both hardware and
software. It maps memorysoftware. It maps memory
addresses used by aaddresses used by a
program, called program, called virtualvirtual
addressesaddresses, into, intophysicalphysical
addressesaddresses in computer in computer
memory. memory.
Direct memory access (DMADirect memory access (DMA) is) is
a feature of computerizeda feature of computerized
systems that allows certainsystems that allows certain
hardware subsystems tohardware subsystems to
access main systemaccess main system
memory independently ofmemory independently of
the central processingthe central processing
unit (CPU).unit (CPU).
5. INDIRECT ADDRESSINGINDIRECT ADDRESSING
An address in a computer instruction that indicates a location where An address in a computer instruction that indicates a location where
the address of the referenced operand is to be found. Alsoknown asthe address of the referenced operand is to be found. Alsoknown as
multilevel address.multilevel address.
7. What isWhat is……
Virtual memory as an alternate set ofVirtual memory as an alternate set of
memory addresses.memory addresses.
Programs use these virtual addressesPrograms use these virtual addresses
rather than real addresses to storerather than real addresses to store
instructions and data.instructions and data.
When the program is actually executed,When the program is actually executed,
the virtual addresses are converted intothe virtual addresses are converted into
real memory addresses.real memory addresses.
8. Why is it neededWhy is it needed……..
Before the development of the virtual memoryBefore the development of the virtual memory
technique, programmers had to manage directlytechnique, programmers had to manage directly
two-level storage such as main memory or ramtwo-level storage such as main memory or ram
and secondary memory in the form of hard disksand secondary memory in the form of hard disks
or earlier, magnetic drums.or earlier, magnetic drums.
Enlarge the address space, the set of addressesEnlarge the address space, the set of addresses
a program can utilize.a program can utilize.
Virtual memory might contain twice as manyVirtual memory might contain twice as many
addresses as main memory.addresses as main memory.
9. ObjectObject……
When a computer is executing many programsWhen a computer is executing many programs
at the same time, Virtual memory make theat the same time, Virtual memory make the
computer to share memory efficiently.computer to share memory efficiently.
Eliminate a restriction that a computer works inEliminate a restriction that a computer works in
memory which is small and be limited.memory which is small and be limited.
When many programs is running at the sameWhen many programs is running at the same
time, by distributing each suitable memory areatime, by distributing each suitable memory area
to each program, VM protect programs toto each program, VM protect programs to
interfere each other in each memory area.interfere each other in each memory area.
10. How does it workHow does it work……
To facilitate copying virtual memory into realTo facilitate copying virtual memory into real
memory, the operating system divides virtualmemory, the operating system divides virtual
memory into pages, each of which contains amemory into pages, each of which contains a
fixed number of addresses.fixed number of addresses.
Each page is stored on a disk until it is needed.Each page is stored on a disk until it is needed.
When the page is needed, the operating systemWhen the page is needed, the operating system
copies it from disk to main memory, translatingcopies it from disk to main memory, translating
the virtual addresses into real addresses.the virtual addresses into real addresses.
11. Virtual MemoryVirtual Memory
Use main memory as a “cache” for secondary memoryUse main memory as a “cache” for secondary memory
Allows efficient and safe sharing of memory among multipleAllows efficient and safe sharing of memory among multiple
programs.programs.
Provides the ability to easily run programs larger than the size ofProvides the ability to easily run programs larger than the size of
physical memoryphysical memory
Simplifies loading a program for execution by providing for codeSimplifies loading a program for execution by providing for code
relocation (i.e., the code can be loaded anywhere in main memory)relocation (i.e., the code can be loaded anywhere in main memory)
What makes it work? – again the Principle of LocalityWhat makes it work? – again the Principle of Locality
A program is likely to access a relatively small portion of its addressA program is likely to access a relatively small portion of its address
space during any period of timespace during any period of time
Each program is compiled into its own address space – a “virtual”Each program is compiled into its own address space – a “virtual”
address spaceaddress space
During run-time eachDuring run-time each virtualvirtual address must be translated to aaddress must be translated to a
physicalphysical address (an address in main memory)address (an address in main memory)
12. MMU (Memory ManagementMMU (Memory Management
Unit)Unit)
The hardware base that makes a virtual memory systemThe hardware base that makes a virtual memory system
possible.possible.
Allows software to reference physical memory by virtualAllows software to reference physical memory by virtual
addresses, quite often more than one.addresses, quite often more than one.
It accomplishes this through the use of page and pageIt accomplishes this through the use of page and page
tables.tables.
Use a section of memory to translate virtual addressesUse a section of memory to translate virtual addresses
into physical addresses via a series of table lookups.into physical addresses via a series of table lookups.
The software that handles the page fault is generally partThe software that handles the page fault is generally part
of an operating system and the hardware that detectsof an operating system and the hardware that detects
this situation.this situation.
13. Segmentation... & paging...Segmentation... & paging...
Segmentation involves the relocation of variable sizedSegmentation involves the relocation of variable sized
segments into the physical address space.segments into the physical address space.
Generally these segments are contiguous units, and areGenerally these segments are contiguous units, and are
referred to in programs by their segment number and anreferred to in programs by their segment number and an
offset to the requested data.offset to the requested data.
A technique used by virtual memory operating systemsA technique used by virtual memory operating systems
to help ensure that the data you need is available asto help ensure that the data you need is available as
quickly as possible.quickly as possible.
Instead of utilizing a segment/offset addressingInstead of utilizing a segment/offset addressing
approach, as seen in segmentation, paging uses a linearapproach, as seen in segmentation, paging uses a linear
sequence of virtual addresses which are mapped tosequence of virtual addresses which are mapped to
physical memory as necessary.physical memory as necessary.
Due to this addressing approach, a single program mayDue to this addressing approach, a single program may
refer to series of many non-contiguous segments.refer to series of many non-contiguous segments.
15. Two Programs Sharing PhysicalTwo Programs Sharing Physical
MemoryMemory
Program 1
virtual address space
main memory
A program’s address space is divided into pages (all one fixed size)
or segments (variable sizes)
The starting location of each page (either in main memory or in
secondary memory) is contained in the program’s page table.
Program 2
virtual address space
16. Address TranslationAddress Translation
Virtual Address
(VA)
Page offsetVirtual page number
31 30 . . . 12 11 . . . 0
Page offsetPhysical page number
Physical Address (PA)
29 . . . 12 11 0
Translation
So each memory requestSo each memory request firstfirst requires an addressrequires an address translationtranslation from thefrom the
virtual space to the physical spacevirtual space to the physical space
A virtual memory miss (i.e., when the page is not in physicalA virtual memory miss (i.e., when the page is not in physical
memory) is called amemory) is called a page faultpage fault
A virtual address is translated to a physical address by
a combination of hardware and software
17. VIRTUAL ADDRESSING WITHVIRTUAL ADDRESSING WITH
CACHECACHE
Thus it takes anThus it takes an extraextra memory accessmemory access
to translate a VA to a PA.to translate a VA to a PA.
CPUTrans-
lation
Cache
Main
Memory
VAPAmiss
hit
data
This makes memory (cache) accesses very expensive (if every
access was really two accesses)
The hardware fix is to use a Translation Lookaside Buffer (TLB) – a
small cache that keeps track of recently used address mappings to
avoid having to do a page table lookup
18. Translation Lookaside BuffersTranslation Lookaside Buffers
(TLBs)(TLBs)
Just like any other cache, the TLB can beJust like any other cache, the TLB can be
organized as fully associative, set associative,organized as fully associative, set associative,
or direct mappedor direct mapped
V Virtual Page # Physical Page # Dirty Ref
Access
TLB access time is typically smaller than cache access
time (because TLBs are much smaller than caches)
TLBs are typically not more than 128 to 256 entries even on high
end machines
19. A TLB in the Memory HierarchyA TLB in the Memory Hierarchy
A TLB miss – is it a page fault or merely a TLB miss?A TLB miss – is it a page fault or merely a TLB miss?
If the page is loaded into main memory, then the TLB miss canIf the page is loaded into main memory, then the TLB miss can
be handled (in hardware or software) by loading the translationbe handled (in hardware or software) by loading the translation
information from the page table into the TLBinformation from the page table into the TLB
Takes 10’s of cycles to find and load the translation info intoTakes 10’s of cycles to find and load the translation info into
the TLBthe TLB
If the page is not in main memory, then it’s a true page faultIf the page is not in main memory, then it’s a true page fault
Takes 1,000,000’s of cycles to service a page faultTakes 1,000,000’s of cycles to service a page fault
TLB misses are much more frequent than true page faultsTLB misses are much more frequent than true page faults
CPU
TLB
Lookup
Cache
Main
Memory
VA PA miss
hit
data
Trans-
lation
hit
miss
¾ t¼ t
20. Summarizing(virtual memory)Summarizing(virtual memory)
……
Virtual memoryVirtual memory is a common part of most operatingis a common part of most operating
systems on computers.systems on computers.
It has become so common because it provides a bigIt has become so common because it provides a big
benefit for users at a very low cost.benefit for users at a very low cost.
benefits of executing a program that is only partially inbenefits of executing a program that is only partially in
memory.memory.
program isprogram is no longer constrainedno longer constrained by the amount ofby the amount of
physical memory.physical memory.
⇒⇒ user would be able to write programs for anuser would be able to write programs for an
extremely large virtual address space.extremely large virtual address space.
more programsmore programs could be run at the same timecould be run at the same time
⇒⇒ increase CPU utilization and throughput.increase CPU utilization and throughput.
less I/Oless I/O would be neededwould be needed to load or swapto load or swap each usereach user
programprogram
⇒⇒ run fasterrun faster
23. Immediate AddressingImmediate Addressing
Operand is part of instructionOperand is part of instruction
Operand = address fieldOperand = address field
e.g. ADD 5e.g. ADD 5
Add 5 to contents of accumulatorAdd 5 to contents of accumulator
5 is operand5 is operand
No memory reference to fetch dataNo memory reference to fetch data
FastFast
Limited rangeLimited range
25. DIRECT ADDRESSINGDIRECT ADDRESSING
Address field contains address of operandAddress field contains address of operand
Effective address (EA) = address field (A)Effective address (EA) = address field (A)
e.g. ADD Ae.g. ADD A
Add contents of cell A to accumulatorAdd contents of cell A to accumulator
Look in memory at address A for operandLook in memory at address A for operand
Single memory reference to access dataSingle memory reference to access data
No additional calculations to work out effectiveNo additional calculations to work out effective
addressaddress
Limited address spaceLimited address space
27. INDIRECT ADDRESSINGINDIRECT ADDRESSING
In this mode the address field of the instruction
gives the address where the effective address
is stored in memory.
Memory cell pointed to by address field
contains the address of (pointer to) the operand
EA = (A)
Look in A, find address (A) and look there
for operand
e.g. ADD (A)
Add contents of cell pointed to by
contents of A to accumulator
28. INDIRECT ADDRESSINGINDIRECT ADDRESSING
CONT...CONT...
Large address space
2n
where n = word length
May be nested, multilevel, cascaded
e.g. EA = (((A)))
Indirect addressing is often combined with pre
or post increment or decrement addressing
allowing the address of the operand to
be increases by one either before or after
using it.
Multiple memory accesses to find operand
Hence slower
31. Register Addressing (1)Register Addressing (1)
Operand is held in register named inOperand is held in register named in
address filedaddress filed
EA = REA = R
Limited number of registersLimited number of registers
Very small address field neededVery small address field needed
Shorter instructionsShorter instructions
Faster instruction fetchFaster instruction fetch
32. Register Addressing (2)Register Addressing (2)
No memory accessNo memory access
Very fast executionVery fast execution
Very limited address spaceVery limited address space
Multiple registers helps performanceMultiple registers helps performance
Requires good assembly programming orRequires good assembly programming or
compiler writingcompiler writing
N.B. C programmingN.B. C programming
register int a;register int a;
c.f. Direct addressingc.f. Direct addressing
34. Register IndirectRegister Indirect
AddressingAddressing
C.f. indirect addressingC.f. indirect addressing
EA = (R)EA = (R)
Operand is in memory cell pointed to byOperand is in memory cell pointed to by
contents of register Rcontents of register R
Large address space (2Large address space (2nn
))
One fewer memory access than indirectOne fewer memory access than indirect
addressingaddressing
36. Displacement AddressingDisplacement Addressing
EA = A + (R)EA = A + (R)
Address field hold two valuesAddress field hold two values
A = base valueA = base value
R = register that holds displacementR = register that holds displacement
or vice versaor vice versa
38. Relative AddressingRelative Addressing
A version of displacement addressingA version of displacement addressing
R = Program counter, PCR = Program counter, PC
EA = A + (PC)EA = A + (PC)
PC holds the address of next instruction toPC holds the address of next instruction to
be read.be read.
Used with branch type instructions.Used with branch type instructions.
Results in shorter address field.Results in shorter address field.
39. Base-Register AddressingBase-Register Addressing
The content of the base address is addedThe content of the base address is added
to the address part of the instructionto the address part of the instruction
It is assumed to hold a base address andIt is assumed to hold a base address and
address field which gives displacementaddress field which gives displacement
relativerelative
The are used in computers for reallocationThe are used in computers for reallocation
of programsof programs
R may be explicit or implicitR may be explicit or implicit
40. Indexed AddressingIndexed Addressing
EA = A + REA = A + R
It is a special type of cpu register.It is a special type of cpu register.
The distance b/w beginning address andThe distance b/w beginning address and
operands address is index valueoperands address is index value
In this mode the content of an indexIn this mode the content of an index
register is added to the address part ofregister is added to the address part of
the register to obtain EAthe register to obtain EA
Good for accessing arraysGood for accessing arrays
Editor's Notes
The page size is 212 = 4KB, the number of physical pages allowed in memory is 218, the physical address space is 1GB and the virtual address space is 4GB