Virtualizing Memory - Segmentation and Paging
Segmentation
- Segment is a contiguous portion of the address space of a particular length.
- Different segments: code, stack, heap.
- Each segment can be placed in different part of physical memory.
- base and bounds exist in each segment

- base and bounds exist in each segment
Address Transition
The code segment starts at the virtual address 0 in address space.
Example:
Code Segment: the offset of virtual address 100 is 100 (starts at virtual address 0).

Heap Segment: the offset of virtual address 4200 is 104 (starts at virtual address 4096).

- Segmentation Fault
- If an illegal Address is referenced, the hardware detects that the address is out of bounds.
Referring to Segment
- Explicit approach
- chop up the address space into segments based on the top few bits of virtual address.
- Example: virtual address 4200 (
01000001101000)[]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16/*
SEG_MASK = 0x3000 (11000000000000)
SEG_SHIFT = 12
OFFSET_MASK = 0xFFF (00111111111111)
*/
// get top 2 bits of 14-bit VA
Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT
// now get offset
Offset = VirtualAddress & OFFSET_MASK
if (Offset >= Bounds[Segment]){
RaiseException(PROTECTION_FAULT)
}else{
PhysAddr = Base[Segment] + Offset
Register = AccessMemory(PhysAddr)
}
- Stack Segment
- Since stack grows backward, an extra hardware support:
- The hardware checks which way the segment grows
1- positive direction,0- negative direction
- Since stack grows backward, an extra hardware support:
- Sharing
- Segment can be shared between address space by an extra hardware support: Production bits
- a few more bit to indicate permissions of read, write and execute in each segment

- a few more bit to indicate permissions of read, write and execute in each segment
- Segment can be shared between address space by an extra hardware support: Production bits
Fragmentation
External Fragmentation: some little holes of free space in the physical memory that make difficulty to allocate new segments.
Compaction: rearranging the exiting segments in physical memory, but it is costly.
- Stop the running process
- Copy data to somewhere
- Change segment register value

Summary
- Segmentation uses the base-bounds pair to represent the location and boundary of process in the physical memory

Paging
- Paging splits up the address space into fixed-sized unit called a page.
- 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.

Advantages
Flexibility:
- Do not need to assume how heap and stack grow and be used.
Simplicity:
- The page in address space and the page frame are same size (usually 4KB).
- Easy to allocate and keep a free list
Address Translation

Two components in the virtual address
- VPN: virtual page number
Offset: offset within the page

- For a 64-byte address space with 16 bytes page.
- We have $4 = 2^2$ pages and and page size $16 = 2^4$.
- Therefore, we can use 6 bits to represent the address, where the first 2 bits are VPN, the rest are the offset.
Example:

Page Table
- The page table is just a data structure (array) that is used to map the virtual address to physical address.
- The OS indexes the array by VPN and looks up the page-table entry.
- Page Table Storage:
- Page tables can get very large:
- 32-bit address ($2^{32} = 4$GB) space with 4-KB pages and 20bits for VPN
($4$MB = $2^{20}$ entries * $2^2$ bytes per table entry)
- 32-bit address ($2^{32} = 4$GB) space with 4-KB pages and 20bits for VPN
- Page tables can get very large:
Common Flags of Page Table Entry
- Valid Bit: Indicating whether the particular transition is valid.
- Protection Bit: Indicating whether the page could be read from, written to, or execute from.
- Present Bit: Indicating whether this page is in physical memory or on disk.
- Dirty Bit: Indicating whether the page has been modified since it was brought into memory.
Reference Bit (Accessed Bit): Indicating that a page has been accessed.

P: present bitR/W: read / write bitU/S: supervisorA: accessed bitD: dirty bitPFN: the page frame number
- Access memory with paging
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20// Extract the VPN from the virtual address
VPN = (VirtualAddress & VPN_MASK) >> SHIFT
// Form the address of the page-table entry (PTE)
PTEAddr = PTBR + (VPN * sizeof(PTE))
// Fetch the PTE
PTE = AccessMemory(PTEAddr)
// Check if process can access the page
if (PTE.Valid == False) {
RaiseException(SEGMENTATION_FAULT)
} else if (CanAccess(PTE.ProtectBits) == False) {
RaiseException(PROTECTION_FAULT)
} else {
// Access is OK: form physical address and fetch it
offset = VirtualAddress & OFFSET_MASK
PhysAddr = (PTE.PFN << PFN_SHIFT) | offset
Register = AccessMemory(PhysAddr)
}
- Disadvantage: Paging is too slow
- To find a location of the target PTE, the starting location of the page table is needed.
- For every memory reference, paging requires the OS to perform one extra memory reference.
Summary
- Paging divides the virtual address and physical memory into pages with same page size.
- Use the page table to store the mapping PFN for each process.



