virtual memory
TRANSCRIPT
![Page 1: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/1.jpg)
VIRTUAL MEMORYPRESENTED BY KAMRAN ASHRAF13-NTU-4009
![Page 2: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/2.jpg)
Outline Virtual Memory
Page faults
Swapping
Paging
Segmentation
Paging in Linux
Cache translation in hardware
Translation Lookaside buffer (TLB)
![Page 3: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/3.jpg)
Goals of OS memory management
Allocate limited memory resources among competing
processes
Maximizing memory
utilization
Provide isolation between processes
![Page 4: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/4.jpg)
Our main questions How is protection enforced?
How are processes relocated?
How is memory partitioned?
![Page 5: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/5.jpg)
Why Virtual Memory? The basic abstraction that the OS provides for memory management is virtual
memory
◦ VM enables programs to execute without requiring their entire address space to be resident in physical memory
◦ program can also execute on machines with less RAM than it “needs”
virtual memory isolates processes from each other
◦ each process has its own isolated address space
![Page 6: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/6.jpg)
Virtual addresses To make it easier to manage memory of multiple processes, virtual addresses are used
◦ virtual addresses are independent of location in physical memory (RAM)
◦ OS determines location in physical memory
virtual addresses are translated by hardware into physical addresses
![Page 7: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/7.jpg)
Swapping
save a program’s entire state (including its
memory image) to disk
allows another program to run
first program can be swapped back in and re-started right where
it was
![Page 8: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/8.jpg)
![Page 9: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/9.jpg)
Fragmentation internal fragmentation:
◦ the available partition is larger than what was requested
external fragmentation:
◦ two small partitions left, but one big job
dealing with fragmentation:◦ Swap a program out
◦ Re-load it, adjacent to another
partition 0
partition 1
partition 2
partition 3
partition 4
partition 0
partition 1
partition 2partition 3
partition 4
![Page 10: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/10.jpg)
Modern technique: Paging Solve the external fragmentation problem by using fixed sized units in both physical and
virtual memory
frame 0
frame 1
frame 2
frame Y
physical address space
…
page 0
page 1
page 2
page X
virtual address space
…page 3
![Page 11: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/11.jpg)
Virtual address space Processes view memory as a contiguous address space from bytes 0 through N
◦ virtual address space (VAS)
In reality, virtual pages are scattered across physical memory frames
◦ virtual-to-physical mapping is invisible to the program
Protection is provided because a program cannot reference memory outside of its VAS
![Page 12: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/12.jpg)
Address translation a virtual address has two parts: virtual page number & offset
◦ virtual page number (VPN) is index into a page table
◦ page table entry contains page frame number (PFN)
◦ physical address is PFN::offset
Page tables
◦ managed by the OS
◦ map virtual page number (VPN) to page frame number (PFN)
![Page 13: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/13.jpg)
Examples:◦ workstations, servers, modern PCs, etc.
Address Translation: Hardware converts virtual addresses to physical addresses via OS-managed lookup table (page table)
CPU
0:1:
N-1:
Memory
0:1:
P-1:
Page Table
Disk
VirtualAddresses
PhysicalAddresses
![Page 14: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/14.jpg)
pageframe 0
pageframe 1
pageframe 2
pageframe Y
…
pageframe 3
physical memory
offset
physical address
page frame #page frame #
page table
offset
virtual address
virtual page #
![Page 15: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/15.jpg)
Page Table Entries (PTEs)
◦ the valid bit says whether or not the PTE can be used◦ says whether or not a virtual address is valid◦ it is checked each time a virtual address is used
◦ the referenced bit says whether the page has been accessed◦ it is set when a page has been read or written to
◦ the modified bit says whether or not the page is dirty◦ it is set when a write to the page has occurred
◦ the protection bits control which operations are allowed◦ read, write, execute
◦ the page frame number determines the physical page◦ physical page start address = PFN
page frame numberprotMRV
202111
![Page 16: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/16.jpg)
Paging advantages Easy to allocate physical memory
◦ physical memory is allocated from free list of frames
◦ external fragmentation is not a problem!
Leads naturally to virtual memory
◦ entire program need not be memory resident
◦ take page faults using “valid” bit
![Page 17: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/17.jpg)
Paging disadvantages Can still have internal fragmentation
◦ process may not use memory in exact multiples of pages
Memory reference overhead
◦ 2 references per address lookup (page table, then memory)
◦ solution: use a hardware cache to absorb page table lookups
![Page 18: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/18.jpg)
Page Faults What if an object is on disk rather than in memory?
◦ Page table entry indicates virtual address not in memory◦ OS exception handler invoked to move data from disk into memory
◦ current process suspends, others can resume◦ OS has full control over placement, etc.
CPU
Memory
Page Table
Disk
VirtualAddresses
PhysicalAddresses
CPU
Memory
Page Table
Disk
VirtualAddresses
PhysicalAddresses
Before After
![Page 19: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/19.jpg)
Example: suppose page size is 4 bytes.
![Page 20: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/20.jpg)
Addressing Page TablesWhere do we store Page Tables? (Which address space)
Physical Memory◦ Easy to address, no translation required◦ But allocated page tables consume memory for life time of VAS
Virtual Memory (OS Virtual address space)◦ Unused page table pages can be paged out to disk◦ But addressing required translation
![Page 21: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/21.jpg)
Segmentation
partition an address space into logical unitsstack, code, data, subroutines, …
![Page 22: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/22.jpg)
Segmentation Vs PagingPAGING
◦ Eases various memory allocation complexities (e.g., fragmentation)
◦ divide it into pages of equal size (e.g., 4KB)◦ use a page table to map virtual pages to
physical page frames◦ page (logical) => page frame (physical)
◦ Fixed size partition
SEGMENTATION
◦ partition an address space into logical units◦ stack, code, heap, subroutines, …
◦ a virtual address is <segment #, offset>◦ Variable size partition
![Page 23: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/23.jpg)
Why Segmentation? More “logical”
◦ Linker takes a bunch of independent modules that call each other and organizes them
Facilitates sharing and reuse◦ a segment is a natural unit of sharing
A natural extension of variable-sized partitions◦ variable-sized partition = 1 segment/process◦ segmentation = many segments/process
![Page 24: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/24.jpg)
Hardware support Segment table
◦ multiple base/limit pairs, one per segment
◦ segments named by segment #, used as index into table
◦ a virtual address is <segment #, offset>
![Page 25: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/25.jpg)
Segment Lookups
segment 0
segment 1
segment 2
segment 3
segment 4
physical memory
segment #
+
virtual address
<?
raise protection fault
no
yes
offset
baselimit
segment table
![Page 26: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/26.jpg)
Pros and Cons
+ efficient for sparse address spaces
+ easy to share whole segments (for example, code segment)
- complex memory allocation
- Still need first fit, best fit, etc., and re-shuffling to coalesce free fragments, if no single free space is big enough for a new segment.
![Page 27: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/27.jpg)
Linux◦ 1 kernel code segment, 1 kernel data segment
◦ 1 user code segment, 1 user data segment
◦ N task state segments (stores registers on context switch)
◦ all of these segments are paged
![Page 28: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/28.jpg)
Segmentation and Paging Can combine segmentation and Paging
Use segments to manage logically related units
Use pages to partition segments into fixed size chunks
Complex as two lookups per memory reference
![Page 29: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/29.jpg)
Segmentation and Paging Translation
![Page 30: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/30.jpg)
Efficient Translation Our original page table scheme already doubled the cost of doing memory lookups
◦ One lookup for page table, another to fetch the data
Now two level page tables triple the cost◦ Two lookups into the page tables, third to fetch the data
How can we use paging and have same lookup cost as fetching from memory?◦ Cache translation in hardware◦ Translation Lookaside buffer (TLB
![Page 31: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/31.jpg)
Integrating Cache
CPUTrans-lation
Cache MainMemory
VA PA miss
hitdata
![Page 32: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/32.jpg)
Speeding up Translation with a TLB
“Translation Lookaside Buffer” (TLB)◦ Small hardware cache in MMU◦ Maps virtual page numbers to physical page numbers◦ Contains complete page table entries for small number of pages
CPUTLBLookup
Cache MainMemory
VA PA miss
hit
data
Trans-lation
hit
miss
![Page 33: VIRTUAL MEMORY](https://reader035.vdocuments.net/reader035/viewer/2022062711/55c6aa6abb61eb0f5d8b477e/html5/thumbnails/33.jpg)
Thanks