CS330 Operating System Part V
CS330 Operating System Part V
Lecture 23
Virtual Memory: Address Translation
Address space abstraction provides the same memory view to all the process. However, this address
space is virtual, and it is the responsibility of the OS, to provide this view. Users use several APIs
to manage this virtual view, and there is no direct control over the physical memory resource.
Note: rbp register is the marker of the start of the stack space of a function. The compiler
does not know the stack address, it uses the rsp, rbp registers to access function stacks.
2
For eg.:
Physical memory of 8KB is allocated and code and data is loaded. The PCB memory state is
updated based on the executable format.
3
What happens during a context switch? Saving and restoring the base and limit registers values
onto the PCB of the process (User to OS context switching).
Advantages: the number of operations for the hardware are very limited. However the
disadvantages are many fold.
Disadvantage: Physical memory must be greater than address space size. This is unrealistic,
and against the philosophy of address space abstraction. This also limits the size of address space.
This, is memory inefficient, there is memory wastage, the unused space between heap and
stack, which is allocated but never used. This also limits the degree of multiprogramming because
limits the physical resource available.
Lecture 24
Virtual Memory: Segmentation
We saw that address space granularity level address translation was memory inefficient, and in-
flexible. We can now extend the scheme to segment level granularity. We would now require more
base-limit pairs. That is different for each segment.
• Explicit addressing: Part of the address is used to specify segment. For eg.: consider a
virtual address space of 8KB, which requires an address length of 13 bits. The first two bits
can be used to specify the segment. For instance, 00 for code, 01 for data, and 11 for stack.
The rest of the bits can be used as offsets.
Issues with this style of addressing:
– Inflexible: max size of segments already fixed.
4
For stack the direction can be negative, for the hardware to calculate addresses.
S bit can be used to specify privilege, specifically in the code segment.
R, W, X can be used to enforce isolation and sharing.
5
Segmentation in reality
Instead of specific segments, there can be multiple segments. And the behaviour of each can be
captured using the flags and other metadata corresponding to it. Descriptor Table Register
(DTR) is used to access the descriptor table. The number of descriptors depends on the ar-
chitecture. We may need separate descriptors for user and kernel mode.
Note: the CS, SS and DS and other segment registers, hold the offset to the information cor-
responding to their entry in the descriptor table.
Changing the privilege is pointing the code segment register to appropriate descriptor offset.
In the case of context switch, we now some more information to store. Specifically the process’
segment registers.
Advantages of Segmentation: The operations regarding address translation are still easy and
straightforward. It saves the memory wastage from unused addresses, which was previously faced
during address space granularity address translation scheme.
Disadvantages: External fragmentation: The physical memory is divided into usable chunks,
however they are distributed throughout, and may cause several small unusable chunks remaining.
Also, it cannot support dis-contiguous sparse mapping.
Lecture 25
Virtual Memory: Paging
The idea behind paging is to partition the address space into fixed sized blocks (called pages).
The physical memory is partitioned into a similar way (page frames).
The responsibility of the OS is to set the mapping between virtual and physical address.
The responsibility of the hardware is to walk through the translation to translate the virtual
address to the physical address.
6
Example
Consider a virtual address space of 32KB, and a page size of 256 bytes. The address length would
be atleast 15 bits, the first 7 bits of which (the MSB 7) can be used to find the page number.
The number of pages being 128. The LSB 8 bits can be used to find the offset within a page. For
instance: VA = 0x0510 corresponds to page number 5 and offset 16.
Consider similarly the physical memory to be of 64KB size, the page frame size (is same as
page size) is 256 bytes, which would mean the number of pages to be 256. The required address
length would thus be 16 bits long, the MS-8-bits to specify the page frame number, and the later
bits to find offset within the page frame. For instance: PA = 0x1F51 corresponds to page frame
number 31 and offset 81.
Paging example
Each entry in the table is called the page table entry PTE.
PTW (vaddr V, PTable P) { // page table walk (by the hardware)
// input: virtual address, page table
// returns: physical address
Entry = P[V >> 8];
if (Entry.present) {
return (Entry.PFN << 8) + (V & 0xFF);
}
Raise PageFault;
}
In the example depicted in the figure, the VA 0x10 translated to PA 0x110. While VA 0x7FF0
translates to PA 0x3F0.
7
Structure of PTE
Hence consider again the Paging example figure, we would have the code segment (Page 0)’s
(which is read and execute mode) PTE as something like 0x125, data segment (Page 2 and 3)’s
(which is read and write) to be 0x207, and ox407. The stack (Page 127)’s (which is again read
and write) to be something like 0x307.
Thus the PTE apart from the PFN, also stores the privilege information, permissions and
other flags.
Lecture 26-27
Virtual memory: Multi-level Paging - I
One level of page tbale may not be enough. Consider the case of a 32-bit system, having a address
space of 4GB.
8
What could be the page size for the address space? Too large address space results in internal
fragmentation. Assuming a page size of 4KB, we have under one-level paging 22 0 entries, which
are not possible to be held in a single page. Therefore multi-level paging is used in modern-day
systems.
For a virtual address of size 248 , and page size and page frame size being 4KB. Four levels of
paging with entry size: 64 bits.
Hardware translation by repeated access of page table stored in physical memory. Inside a
page table entry, we have the 12bits (LSB) for the access flags.
Paging efficiency
Consider a simple loop:
sum = 0;
for (int i = 0; i < n; i++)
sum += i;
OS cannot make entries in the TLB, but can flush out entries. There can be separate TLBs
for instruction and data, multi-level TLBs.
Page fault
Page faults are required to support memory-over-commitment through lazy allocation and swap-
ping. Specific error codes are pushed into the kernel stack in x86.
If( !pte.valid ||
(access == write && !pte.write) ||
(cpl != 0 && pte.priv == 0)){
CR2 = Address;
errorCode = pte.valid
| access << 1
| cpl << 2;
Raise pageFault;
} // Simplified
The hardware invokes the page fault handler by handing the error code and virtual address.
The OS handles the page fault by either fixing it or raising a SEGFAULT.
HandlePageFault( u64 address, u64 error_code)
{
if ( AddressExists(current -> mm_state, address) &&
AccessPermitted(current -> mm_state, error_code) {
PFN = allocate_pfn( );
install_pte(address, PFN);
return;
}
RaiseSignal(SIGSEGV);
}
Lecture 28
Page fault and Swapping
When the number of free PFNs may be running low and an allocate_pfn() might be called, the
OS uses its page replacement policy to swap out certain pages onto the hard disk from the
DRAM.
A methodology: update the present-bit to 0, in the PTE such that any access to the page
through the virtual address will result in a page fault. Also the swap address has to be maintained
11
in the PTE. Content of the PFN is now in the swap device. In future, any translation using the
PTE will result in a page fault. The page fault handler would copy it back from the swap device.
Page replacement
Objective: is to minimize the number of page faults (due to swapping)
The three parameters are: (i) A given sequence of access to virtual pages, (ii) number of
memory pages, (iii) Page replacement policy.
Metrics to measure the effectiveness: number of page faults, page fault rate, average memory
access time.
After swapping out the TLB has to be flushed out. Swapping is initiated by the OS. We need
that S bit to know that it is a swapped out page, as page fault might not have involved swapping.
Implementation of LRU using a single accessed-bit may not be practical, can be approximated
using CLOCK (a useful approximation to LRU, in which pages can be placed on a dial of clock
in which the first not swapped (while going clockwise/counter-clockwise) is swapped.
Follows Stack property or inclusion property of eviction algorithms (also the reason
behind Belady’s anomaly): Let A = {a1 , a2 , ..., an } be the access order and p be the cache-size and
at some point k, we have the contents of the stack as S, if for another cache size q(< p), at the
′ ′
same point k, the contents of the stack S . If S ⊆ S, then the eviction rule follows the property.