Paging
Paging
COMS(3010A)
Paging
Branden Ingram
[email protected]
Recap
• Memory Virtualisation
• Address Translation
• Segmentation
• Free Space Management
Paging
• The main idea behind the paging is to divide each process(Address Space) into a
fixed-sized unit called a page
Paging
• The main idea behind the paging is to divide each process(Address Space) into a
fixed-sized unit called a page
• The main idea behind the paging is to split each process(Address Space) into a
fixed-sized unit called a page
• With paging, physical memory is also split into some number of pages called a page
frame.
Paging Translation ?
• Page table per process is needed to translate the virtual address to physical address.
Segment Register
• Page table per process is needed to translate the virtual address to physical address.
Page Table
Segment Register Page Page Frame
Segment Base Size 0 3
Code 256KB 4KB 1 7
Heap 260KB 60KB
Stack 128KB 64KB 2 5
3 2
Advantages of Paging
VPN Offset
Address Translation : Example
VPN Offset
0 1 0 1 0 1
VPN Offset
Address Translation : Example
Virtual Address 0 1 0 1 0 1
VPN Offset
Address Translation : Example
Virtual Address 0 1 0 1 0 1
VPN Offset
Virtual Address 0 1 0 1 0 1
VPN Offset
Virtual Address 0 1 0 1 0 1
VPN Offset
Virtual Address 0 1 0 1 0 1
VPN Offset
Address Translation
1 1 1 0 1 0 1
Virtual Address 0 1 0 1 0 1
VPN Offset
Address Translation
1 1 1 0 1 0 1
• Page table per process is needed to translate the virtual address to physical address.
Page Table
32
Page Table 3 Page 3 Page Frame 2
48
Page Table 4 Page 0 Page Frame 3
64
Free Page Frame 4
80
Page 2 Page Frame 5
96
Free Page Frame 6
112
Page 1 Page Frame 7
128
64 byte Address Space placed in
Physical Memory
What is in the Page Table?
• The page table is just a data structure that is used to map the virtual address to the
physical address
Page Table
• The page table is just a data structure that is used to map the virtual address to the
physical address
Page Table
0 3
1 7
2 5
3 2
PWT
R/W
PCD
U/S
PAT
PFN Avail
D
A
P
• P: present bit
• R/W: read/write bit
• U/S: supervisor
• A: accessed bit
• D: dirty bit
• PFN: the page frame number
Page Table Entry : Example x86
PWT
R/W
PCD
U/S
PAT
PFN Avail
D
A
P
• Valid Bit(P): Indicating whether the particular translation is valid.
• Dirty Bit(D): Indicating whether the page has been modified since it was
brought into memory
• Therefore, for every memory reference, paging requires the OS to perform one
extra memory reference
Accessing Memory with Paging
10 else
11 // Access is OK: form physical address and fetch it
12 offset = VirtualAddress & OFFSET_MASK
13 PhysAddr = (PTE.PFN << PFN_SHIFT) | offset
14 Register = AccessMemory(PhysAddr)
Accessing Memory with Paging
int array[1000];
...
for(i = 0; i < 1000; i++)
array[i] = 0;
Page Table(PA)
1174
1124
Page Table[1]
1074
1024
A Virtual( And Physical ) Memory Trace
1224
Page Table[39]
Page Table(PA)
1174
1124
Page Table[1]
1074
1024
jne
cmp
1124 4196
mov
inc
Code(VA)
Code(PA)
1074 4146
1024 4096
0 10 20 30 40 50
Memory Access
A Virtual( And Physical ) Memory Trace
1224
Page Table[39]
Page Table(PA)
1174
1124
Page Table[1]
1074
1024
40100 7132
Array(VA)
Array(PA)
mov
40050 7282
40000 7232
jne
cmp
1124 4196
mov
inc
Code(VA)
Code(PA)
1074 4146
1024 4096
0 10 20 30 40 50
Memory Access
A Virtual( And Physical ) Memory Trace
1224
Page Table[39]
Page Table(PA)
1174
1124
Page Table[1]
1074
1024
40100 7132
Array(VA)
Array(PA)
mov
40050 7282
40000 7232
jne
cmp
1124 4196
mov
inc
Code(VA)
Code(PA)
1074 4146
1024 4096
0 10 20 30 40 50
Memory Access
A Virtual( And Physical ) Memory Trace
1224
Page Table[39]
Page Table(PA)
1174
1124
Page Table[1]
1074
1024
40100 7132
Array(VA)
Array(PA)
mov
40050 7282
40000 7232
jne
cmp
1124 4196
mov
inc
Code(VA)
Code(PA)
1074 4146
1024 4096
0 10 20 30 40 50
Memory Access
Paging is Slow?
• Therefore, for every memory reference, paging requires the OS to perform one
extra memory reference
MMU
Base Register
Bounds Register
Translation Lookaside Buffers
MMU MMU
Logical
Address
CPU
MMU
TLB
Logical
Page Table Base
Address
Register
CPU
MMU
TLB
Logical
Page Table Base
Address
Register Page Table
Page 0
MMU Page 1
Page 2
…
TLB
Page n Physical
Address
Logical Physical Memory
Page Table Base
Address
Register Page Table
Logical
Address
CPU
MMU
TLB
Stores popular v to p
Logical
Address
Page 0
MMU
Page 1
Page 2
TLB …
TLB Hit
Stores popular v to p Page n
Logical Physical Memory
Address
Page 0
MMU
Page 1
Page 2
TLB …
Stores popular v to p Page n Physical
Address
Logical Physical Memory
TLB Miss
Address
Page Table
Page Table Base
Register Page Page Frame
CPU 0 3
1 7
2 5
3 2
Address Translation with TLB
TLB ALgortihm
• (12-13) The hardware accesses the page table to find the translation.
• (19) updates the TLB with the translation.
A TLB Entry
Page 0
MMU
Page 1
Page 2
TLB …
Stores popular v to p Page n Physical
Address
Logical Physical Memory
TLB Miss
Address
Page Table
Page Table Base
Register Page Page Frame
CPU 0 3
1 7
2 5
3 2
Address Translation with TLB
A TLB Entry
0: int sum = 0 ;
1: for( i=0; i<10; i++){
2: sum+=a[i];
3: }
Example : Accessing an Array
OFFSET
00 04 08 12 16
0: int sum = 0 ;
VPN = 00
VPN = 01
1: for( i=0; i<10; i++){
VPN = 03 2: sum+=a[i];
VPN = 04 3: }
VPN = 05
VPN = 11
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET
00 04 08 12 16
0: int sum = 0 ;
VPN = 00
VPN = 01
1: for( i=0; i<10; i++){
VPN = 03 2: sum+=a[i];
VPN = 04 3: }
VPN = 05
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 04
VPN = 05
OFFSET TLB
00 04 08 12 16
VPN = 04
VPN = 05
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 04
VPN = 05
OFFSET TLB
00 04 08 12 16
VPN = 04
VPN = 05
VPN = 11
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 05
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 05
VPN = 11
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 05
VPN = 11
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 05
VPN = 11
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 11
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 11
VPN = 12
VPN = 13
VPN = 14
VPN = 15
Example : Accessing an Array
OFFSET TLB
00 04 08 12 16
VPN = 11
The TLB improves performance
due to spatial locality
VPN = 12
VPN = 13
3 misses and 7 hits.
VPN = 14 Thus TLB hit rate is 70%.
VPN = 15
Locality
• Temporal Locality
• An instruction or data item that has been 1st access is page1.
2nd access is also page1.
recently accessed will likely be re-accessed
soon in the future
Page n
Page 6
Page 7
Page 1
Page 2
Page 3
Page 4
Page 5
…
Virtual Memory
Locality
• Temporal Locality
• An instruction or data item that has been 1st access is page1.
2nd access is also page1.
recently accessed will likely be re-accessed
soon in the future
Page n
Page 6
Page 7
Page 1
Page 2
Page 3
Page 4
Page 5
…
Virtual Memory
• Spatial Locality
• If a program accesses memory at
address x, it will likely soon access 1st access is page1.
memory near x 2nd access is near by page1.
Page n
Page 1
Page 2
Page 3
Page 4
Page 5
…
Virtual Memory
Who handles the TLB Miss?
Page 0
Page 1
Page 2 Insert TLB Entry
access VPN10
Process A …
Page n TLB Table
Page 0
Page 1
Page 2
Process A …
Page n TLB Table
VPN PFN valid prot
Virtual Memory
Context 10 100 1 rwx
Switching - - - -
Page 0 10 170 1 rwx
Page 1 - - - -
access VPN10
Process B Page 2 Insert TLB Entry
…
Page n
Virtual Memory
TLB Issue : Context Switching
Page 0
Page 1
Page 2
Process A …
Page n TLB Table
VPN PFN valid prot
Virtual Memory
10 100 1 rwx
- - - -
Page 0 10 170 1 rwx
Page 1 - - - -
Process B Page 2
… Can’t Distinguish which entry is
Page n meant for which process
Virtual Memory
TLB Issue : Page Sharing
Page 1 - - - - -
Process B Page 2
…
Page n
Virtual Memory
What happens when we want to add a new
entry
• Problem – We need to add a new entry to the TLB
• Well where do we put it and if needed which entry do we replace
TLB Issue : Replacement Policy
• LIFO
• FIFO
• Random
• LRU
TLB Issue : Replacement Policy - Solution
• Add a register to every page frame - contain the last time that the page in that
frame was accessed
• Use a "logical clock" that advance by 1 tick each time a memory reference is
made.
• Each time a page is referenced, update its register
TLB Issue : Replacement Policy - Solution
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0
1 7 1 7 1 7 4 2 4 2 4 2 4 2 8 4 8 4 8 4 11 0 11 0 11 0 14 1 14 1 14 1
Page Frame:
2 0 2 0 2 0 5 0 5 0 7 0 7 0 7 0 10 3 10 3 12 3 12 3 12 3 12 3 16 0
3 1 3 1 3 1 6 3 6 3 6 3 9 2 9 2 9 2 9 2 13 2 13 2 15 2 15 2
PFN C D V
Flag Content