OS Midterm: Virtualization

Ruixuan Tu (rtu7@wisc.edu), University of Wisconsin-Madison

Covers: Through Chapter 22 of OSTEP except Chapters 9, 10, 17.

Processes

stateDiagram-v2 Running --> Ready : descheduled Ready --> Running : scheduled Running --> Blocked : I/O or condition initiate Blocked --> Ready: I/O or condition done Embryo --> Ready: process created Running --> Zombie: process exited but no wait()
stateDiagram-v2 U: User Mode K: Kernel Mode T: Trap Table U --> K : trap K --> U : return-from-trap T

Scheduling

Memory Management

Base and Bounds or Dynamic Relocation

Segmentation

Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT
if (!NegGrowth[Segment]) // Not Stack
  Offset = VirtualAddress & OFFSET_MASK
  if (Offset >= Bounds[Segment])
    RaiseException(PROTECTION_FAULT)
  else
    PhysAddr = Base[Segment] + Offset
else
  NegOffset = (VirtualAddress & OFFSET_MASK) - MAX_SEG_SIZE // < 0
  if (-NegOffset > Bounds[Segment])
    RaiseException(PROTECTION_FAULT)
  else
    PhysAddr = Base[Segment] + NegOffset
Register = AccessMemory(PhysAddr)

Paging: Linear Page Table

VPN = (VirtualAddress & VPN_MASK) >> SHIFT
PTEAddr = PTBR + (VPN * sizeof(PTE))
PTE = AccessMemory(PTEAddr)
if (PTE.Valid == False)
  RaiseException(SEGMENTATION_FAULT)
else if (CanAccess(PTE.ProtectBits) == False)
  RaiseException(PROTECTION_FAULT)
else
  offset = VirtualAddress & OFFSET_MASK
  PhysAddr = (PTE.PFN << PFN_SHIFT) | offset
  Register = AccessMemory(PhysAddr)

Paging: TLB

VPN = (VirtualAddress & VPN_MASK) >> SHIFT
(Success, TlbEntry) = TLB_Lookup(VPN)
if (Success == True) // TLB Hit
  if (CanAccess(TlbEntry.ProtectBits) == True)
    Offset = VirtualAddress & OFFSET_MASK
    PhysAddr = (TlbEntry.PFN << SHIFT) | Offset
    Register = AccessMemory(PhysAddr)
  else
    RaiseException(PROTECTION_FAULT)
else // TLB Miss
  PTEAddr = PTBR + (VPN * sizeof(PTE))
  PTE = AccessMemory(PTEAddr)
  if (PTE.Valid == False)
    RaiseException(SEGMENTATION_FAULT)
  else if (CanAccess(PTE.ProtectBits) == False)
    RaiseException(PROTECTION_FAULT)
  else
    TLB_Insert(VPN, PTE.PFN, PTE.ProtectBits)
    RetryInstruction()

Paging: Smaller Pages

SN = (VirtualAddress & SEG_MASK) >> SN_SHIFT // Segment Bits
VPN = (VirtualAddress & VPN_MASK) >> VPN_SHIFT
AddressOfPTE = Base[SN] + (VPN * sizeof(PTE))
VPN = (VirtualAddress & VPN_MASK) >> SHIFT
(Success, TlbEntry) = TLB_Lookup(VPN)
if (Success == True) // TLB Hit
  if (CanAccess(TlbEntry.ProtectBits) == True)
    Offset = VirtualAddress & OFFSET_MASK
    PhysAddr = (TlbEntry.PFN << SHIFT) | Offset
    Register = AccessMemory(PhysAddr)
  else
    RaiseException(PROTECTION_FAULT)
else // TLB Miss
  PDIndex = (VPN & PD_MASK) >> PD_SHIFT
  PDEAddr = PDBR + (PDIndex * sizeof(PDE))
  PDE = AccessMemory(PDEAddr)
  if (PDE.Valid == False)
    RaiseException(SEGMENTATION_FAULT)
  else
    PTIndex = (VPN & PT_MASK) >> PT_SHIFT
    PTEAddr = (PDE.PFN << SHIFT) + (PTIndex * sizeofPTE))
    PTE = AccessMemory(PTEAddr)
    if (PTE.Valid == False)
      RaiseException(SEGMENTATION_FAULT)
    else if (CanAccess(PTE.ProtectBits) == False)
      RaiseException(PROTECTION_FAULT)
    else
      TLB_Insert(VPN, PTE.PFN, PTE.ProtectBits)
      RetryInstruction()

Swapping

P = RandPage() // clock hand
if (P.Used == True)
  P.Used = False
  if (P == len(AllPages))
    P = 0 // back to front
  else
    P++
else
  Evict(P)
PFN = FindFreePhysicalPage()
if (PFN == -1) // no free page found
  PFN = EvictPage() // run replacement algorithm
DiskRead(PTE.DiskAddr, PFN) // sleep (waiting for I/O)
PTE.present = True // update page table with present bit
PTE.PFN = PFN // bit and translation (PFN)
RetryInstruction() // retry instruction

Other

fully-associative: there are no restrictions on where in memory a page can be placed

Fast Base Conversion

DEC HEX BIN
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111

Power of 2

nn 2n2^n
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1,024
11 2,048
12 4,096
13 8,192
14 16,384
15 32,768
16 65,536
17 131,072
18 262,144
19 524,288
20 1,048,576
21 2,097,152
22 4,194,304
23 8,388,608
24 16,777,216
25 33,554,432
26 67,108,864
27 134,217,728
28 268,435,456
29 536,870,912
30 1,073,741,824