Professional Documents
Culture Documents
10 Virtualization Memory Segmentation Paging
10 Virtualization Memory Segmentation Paging
0KB
P ro g ra m C o d e
5KB Heap
Hard to run when an address space does not fit into
6KB
physical memory
(fre e )
(free)
14KB S ta c k
15KB Stack
16KB
Segmentation
0KB
O p e r a t in g S y s t e m
Operating System
16KB ( n o t in u s e )
(not in use)
Segment Base Size
Code 32K 2K
Heap 34K 2K
Stack 28K 2K
(n o t in u se )
Code 32K 2K
(not in use)
32KB Heap 34K 2K
Code
Code
( n o t in u s e )
48KB
(not in use)
64KB
Physical Memory
𝑝h𝑦𝑠𝑖𝑐𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠=𝑜𝑓𝑓𝑠𝑒𝑡+𝑏𝑎𝑠𝑒
𝑝h𝑦𝑠𝑖𝑐𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠=𝑜𝑓𝑓𝑠𝑒𝑡+𝑏𝑎𝑠𝑒
The offset of virtual address 100 is 100.
The code segment starts at virtual address 0 in address space.
16KB
Code 32K 2K
(not in use)
0KB P ro g ra m C o d e
Code
34KB
Heap
4KB ( n o t in u s e )
(not in use)
Address Translation on Segmentation(Cont.)
Heap 34K 2K
(not in use)
Code
32KB
Code
4KB 34KB is the desired
H eap
H eap
4200 data
Heap physical address
Heap
6KB ( n o t in u s e )
36KB
(not in use)
Address Space
Physical Memory
Segmentation Fault or Violation
If an illegal address such as 7KB which is beyond the end of heap is referenced,
the OS occurs segmentation fault.
The hardware detects that the address is out of bounds.
4KB H eap
Heap
6KB ( n o t in u s e )
7KB
(not in use)
8KB
Address Space
Referring to Segment
Explicit approach
Chop up the address space into segments based on the top few bits of virtual address.
13 12 11 10 9 8 7 6 5 4 3 2 1 0
13 12 11 10 9 8 7 6 5 4 3 2 1 0
Segment Offset
Example: virtual address 4200 (01000001101000)
13 12 11 10 9 8 7 6 5 4 3 2 1 0
Segment bits
Code 00
Heap 01
Stack 10
- 11
Segment bits 0
13 12 11 10 1 0 0 0
9 0
8 0
7 1
6 1
5 0
4 1
3 0
2 0
1 0
0
Code 00 0 1 0 0 0 0 0 1 1 0 1 0 0 0
Heap 01
Stack 10
- 11 Segment Offset
Referring to Segment(Cont.)
1 // get top 2 bits of 14-bit VA
2 Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT
3 // now get offset
4 Offset = VirtualAddress & OFFSET_MASK
5 if (Offset >= Bounds[Segment])
6 RaiseException(PROTECTION_FAULT)
7 else
8 PhysAddr = Base[Segment] + Offset
9 Register = AccessMemory(PhysAddr)
SEG_MASK = 0x3000(11000000000000)
SEG_SHIFT = 12
( n o t in u s e )
Segment Base Size Grows Positive?
Code
Heap
Stack
32K
34K
28K
2K
2K
2K
1
1
0
Stack 28K 2K 0
Stack
28KB ( n o t in u s e )
(not in use)
Physical Memory
Support for Sharing
16KB ( n o t in u s e )
16KB A llo c a t e d
(not in use)
24KB A llo c a t e d
24KB
Allocated Allocated
32KB ( n o t in u s e )
32KB
A llo c a t e d
(not in use)
( n o t in u s e )
48KB 48KB
(not in use)
(not in use)
56KB A llo c a t e d
56KB
Allocated
64KB 64KB
Part II: Paging
Concept of Paging
With paging, physical memory is also split into some number of pages called a
page frame.
Page table per process is needed to translate the virtual address to physical
address.
Paging
P1 P2 P3
Virt Mem
Phys Mem
Fill in Page Table
P1 P2 P3
Virt Mem
Phys Mem
0 1 2 3 4 5 6 7 8 9 10 11
P1 P2 P3
3 0 8
1 4 5
Page Tables: 7 2 9
10 6 11
Advantages Of Paging
reserved for OS
16 (u n u s e d )
physical mem-
(unused) page frame 1
ory
32
0
(page 0 of page 3 of AS page frame 2
16 the 48
address
(page space) page frame 3
page 0 of AS
32 1) 64 (u n u s e d )
(page
(unused) page frame 4
48 2)
80
(page
64 3) page 2 of AS page frame 5
96
A Simple 64-byte Address Space
(u n u s e d )
VPN offset
V a5 V a4 V a3 V a2 V a1 V a0
0 1 0 1 0 1
0 1 0 1 0 1
Example: Address Translation
VPN offset
0 1 0 1 0 1
Virtual
0 1 0 1 0 1
Address
Address
Translation
Address
Translation
1 1 0 1 0 1
1
Physical
1 1 1 0 1 0 1
Address
PFN offset
Where Are Page Tables Stored?
0 p a g e t a b le
3 7 5 2
The page table is just a data structure that is used to map the virtual address to
physical address.
Simplest form: a linear page table, an array
The OS indexes the array by VPN, and looks up the page-table entry.
Common Flags Of Page Table Entry
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 9 8 7 6 5 4 3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
PCD
P AT
U /S
R /W
PW
PFN
P
R/W
PCD
U/S
PAT
PW
PFN
D
G
P
T
P: present An x86 Page Table Entry(PTE)
To find a location of the desired PTE, the starting location of the page table is
needed.
For every memory reference, paging requires the OS to perform one extra
memory reference.
Accessing Memory With Paging
Segmentation
A program contains logically-different segments: code, stack, heap
Paging
physical memory is split into some number of pages called a page frame.
Page table per process is used to translate the virtual address to physical address.