![Page 1: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/1.jpg)
Paging and Virtual Memory
![Page 2: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/2.jpg)
Memory management: Review
Fixed partitioning, dynamic partitioning
ProblemsInternal/external fragmentationA process can be loaded only if a contiguous memory chunk is available to accommodate the processProcess size is limited by the main memory size
Advantage: simplicity
![Page 3: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/3.jpg)
Paging Process memory is divided into fixed
size chunks of the same size, called pages
Pages are mapped onto frames in the main memory
Process pages can be scattered all over the main memory
![Page 4: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/4.jpg)
Paging example01
2
3
4
5
6
7
8
9
10
11
12
13
14
A.0
A.1
A.2
A.3
C.0
C.1
C.2
C.3
D.0
D.1
D.2
D.3
D.4
0
1
2
3
Process A
0
1
2
3
0
1
2
Process B
---
---
---
0
1
2
3
4
0
1
2
3
Process C
7
8
9
10
4
5
6
11
12
Process D
Free Frame List
13
14
![Page 5: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/5.jpg)
Paging support Page table maintains mapping of
process pages onto frames Hardware support is needed to
support translation of relative addresses within a program (logical addresses) into the memory addresses
![Page 6: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/6.jpg)
Address translation Page (frame) size is
a power of 2with page size = 2r, a logical address of l+r bits is interpreted as a tuple (l,r)l = page number, r = offset within the page
Page number is used as an index into the page table
![Page 7: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/7.jpg)
Hardware support
Program Paging Main Memory
Logical address
Register
Page Table
PageFrame
Offset
P#
Frame #
Page Table Ptr
Page # Offset Frame # Offset
+
![Page 8: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/8.jpg)
Virtual Memory Paging makes virtual memory
possibleLogical to physical address mapping is dynamic
=> It is not necessary that all of the process pages be in main memory during execution
![Page 9: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/9.jpg)
Benefits More processes may be maintained
in the main memoryBetter system utilization and throughput
The process size is not restricted by the physical memory size: the process memory is virtual
But what is the limit anyway?
Less disk I/O to swap/load programs
![Page 10: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/10.jpg)
How does this work? CPU can execute a process as long as
some portion of its address space is mapped onto the physical memory
E.g., next instruction and data addresses are mapped
Once a reference to an unmapped page is generated (page fault):
Page fault interrupt transfers control to the OS handler
![Page 11: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/11.jpg)
Page Fault Handler Put the process into blocking state Program disk controller to read the
page from disk into the memory Later on: I/O interrupt signals
completion Resume the process
![Page 12: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/12.jpg)
Why is this practical? Observation: Program branching and
data access patterns are not random Principle of locality: program and
data references tend to cluster=> Only a fraction of the process
virtual address space need to be resident to allow the process to execute for sufficiently long
![Page 13: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/13.jpg)
Virtual memory implementation
Efficient run-time address translationHardware support, control data structures
Fetch policyDemand paging: page is brought into the memory only when page-fault occursPre-paging: pages are brought in advance
Page replacement policyWhich page to evict when a page fault occurs?
![Page 14: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/14.jpg)
Thrashing A condition when the system is
engaged in moving pages back and forth between memory and disk most of the time
Bad page replacement policy may result in thrashing
Programs with non-local behavior
![Page 15: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/15.jpg)
Address translation Virtual address is divided into page
number and offset
Mapping of virtual pages onto physical frames are facilitated by page table(s)
Forward-mapped page tables (FMPT)Inverted page tables (IPT)
Virtual Address
Page Number Offset
![Page 16: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/16.jpg)
Forward-mapped page tables (FMPT)
Page table entry (PTE) structure
Page table is an array of the above
Index is the virtual page number
P M Frame NumberOther Control Bits
Page Table
Frame #
Page #
P: present (valid) bitM: modified bit
![Page 17: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/17.jpg)
Address Translation using FMPT
Program Paging Main Memory
Virtual address
Register
Page Table
PageFrame
Offset
P#
Frame #
Page Table Ptr
Page # Offset Frame # Offset
+
![Page 18: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/18.jpg)
Handling large address spaces One level FMPT is not suitable for
large virtual address spaces32 bit addresses, 4K (212) page size, 232 / 212 = 220 entries ~4 bytes each =>
4Mbytes resident page table per process!What about 64 bit architectures??
Solutions: multi-level FMPTInverted page tables (IPT)
![Page 19: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/19.jpg)
Multilevel FMPT Use bits of the virtual address to
index a hierarchy of page tables The leaf is a regular PTE Only the root is required to stay
resident in main memoryOther portions of the hierarchy are subject to paging as regular process pages
![Page 20: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/20.jpg)
Two-level FMPTpage number page offset
pi p2 d
10 10 12
![Page 21: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/21.jpg)
Two-level FMPT
![Page 22: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/22.jpg)
Inverted page table (IPT) A single table with one entry per
physical page Each entry contains the virtual
address currently mapped to a physical page (plus control bits)
Different processes may reference the same virtual address values
Address space identifier (ASID) uniquely identifies the process address space
![Page 23: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/23.jpg)
Address translation with IPT Virtual address is first indexed into
the hash anchor table (HAT) The HAT provides a pointer to a
linked list of potential page table entries
The list is searched sequentially for the virtual address (and ASID) match
If no match is found -> page fault
![Page 24: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/24.jpg)
Address translation with IPT
Virtual addresspage number offset
hash
+
HAT baseregister
ASID
register
page numberASID
Frame#
IPT
+
IPT baseregister
frame number
HAT
![Page 25: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/25.jpg)
Translation Lookaside Buffer (TLB)
With VM accessing a memory location involves at least two intermediate memory accesses
Page table access + memory access
TLB caches recent virtual to physical address mappings
ASID or TLB flash is used to enforce protection
![Page 26: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/26.jpg)
TLB internals TLB is associative, high speed memory
Each entry is a pair (tag,value)When presented with an item it is compared to all keys simultaneouslyIf found, the value is returned; otherwise, it is a TLB missExpensive: number of typical TLB entries: 64-1024Do not confuse with memory cache!
![Page 27: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/27.jpg)
Address translation with TLB
![Page 28: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/28.jpg)
Bits in the PTE: Present (valid) Present (valid) bit
Indicates whether the page is assigned to frame or notA reference to an invalid page generates page fault which is handled by the operating system
![Page 29: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/29.jpg)
Bits in PTE: modified, used Modified (dirty) bit
Indicates whether the page has been modifiedUnmodified pages need not be written back to the disk when evicted
Used bitIndicates whether the page has been accessed recentlyUsed by the page replacement algorithm
![Page 30: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/30.jpg)
Bits in PTE Access permissions bit
indicates whether the page is read-only or read-write
UNIX copy-on-write bitSet whether more than one process shares a pageIf one of the processes writes into the page, a separate copy must first be made for all other processes sharing the pageUseful for optimizing fork()
![Page 31: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/31.jpg)
Protection with VM Preventing processes from
accessing other process pages Simple with FMPT
Load the process page table base address into a register upon context switch
ASID with IPT
![Page 32: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/32.jpg)
Page size considerations Small page size
better approximates localitylarge page tablesinefficient disk transfer
Large page sizeinternal fragmentation
Most modern architectures support a number of different page sizes
a configurable system parameter
![Page 33: Paging and Virtual Memory. Memory management: Review Fixed partitioning, dynamic partitioning Problems Internal/external fragmentation A process can](https://reader035.vdocument.in/reader035/viewer/2022062320/56649d4a5503460f94a279c1/html5/thumbnails/33.jpg)
Next: Page replacement